Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Hi Guys, can anyone help an absolute python novice?

Hi Guys,

I hope someone can help.

I am trying my best to teach myself to code python and just copying over simple projects to begin with but this one is causing me a bit of headache because it will not run.

If anyone can help me with the below code I would greatly appreciate their tuime and advice.

Sorry its so basic but I have only started over the last few weeks.

Thanks


Code:
import random

attempts_list = []

def show_score ():
  if len(attempts_list) <= 0:
    print ("There is currently no high score, it's yours for the taking!")
  else:
      print("The current high score is {} attempts".format(min(attempts_list)))
      
      
def start_game():
  random_number = int(random.randint(1,10))
  print("Hello traveller! Welcome to the game of guesses!")
  player_name = input("What is your name? ")
  wanna_play = input ("Hi, {}, would you like to play the guessing game? (Enter Yes/No)".format(player_name))
 
attempts = 0

show_score()

while wanna_play.lower() == "yes":
    
  try:
    guess = input("Pick a number between 1 and 10 ")
    if int(guess) < 1 or int(guess) > 10:
      raise ValueError("Please guess a number within the given range")
      if int(guess) == random_number:
        print ("Nice! You got it!")
        attempts += 1
        attempts_list.append(attempts)
        print ("It took you {} attempts".format(attempts))
        play_again = input ("Would you like to play again? (Enter Yes/No) ")
        attempts = 0
        show_score()
        random_number = int(random.randint(1,10))
        if play_again.lower() =="no":
          print ("That's cool, have a good one!")
          break
        elif int(guess) > random_number:
          print ("It's lower")
          attempts += 1
        elif int(guess) < random_number:
            print ("It's higher")
            attempts += 1
  except ValueError as err:
              print ("Oh no!, that is not a valid value. Try again...")
              print ("({})".format(err))
  else:
                print ("That's cool, have a good one!")
                if __name__ == '__main__':

                  start_game()
 
I haven't really took a good look at your code yet but, I did do a quick example of the number guessing game.
When I get back from town, I will look through your code.

Python:
# Do the imports
from random import randint
import os
# Guess list for adding scores
guess_list = []
# Set counter to 0
guesses = 0
# Function for clearing terminal screen
def clear():
    os.system('cls' if os.name == 'nt' else 'clear')
# Function for displaying a score message
def show_score():
    if len(guess_list) > 0:
        return f'The current high score is {min(guess_list)}'
    return f'There is currently no high score. It\'s yours for the taking.'
# Function to generate a random nuber
def number():
    return randint(1, 10)
# Function for a goodbye message
def goodbye(player):
    clear()
    return f'{show_score()}\nGoodbye {player}! Thanks for playing.'
# Display message and get a player name. Defaults to Unknown
print('Hello travler! Welcome to the game of guesses.\n')
player = input('What is your name?\n>> ')
player = 'Unknown' if not player else player
clear()
# Ask player if they want to play
play = input(f'Hi {player}, would you like to play the guessing game? (Enter yes/no)\n>> ')
# A way to exit if they don't want to play
if play.lower().startswith('n'):
    clear()
    print(goodbye(player))
    exit()
else:
    clear()
# Get a random number and start the loop
random_number = number()
while True:  
    # Try/Except block to ensure that only whole numbers are entered  
    try:
        guess = int(input('Pick a number from 1 - 10\n>> '))
    except ValueError:
        clear()
        print('You can only enter whole numbers.') 
        continue
    # If guess is not in bounds of 1 - 10, throw error
    if guess < 0 or guess > 10:
        clear()
        print(f'{player}, {guess} is not within the range.')
    # If guess is lower than random number, increase counter by 1 and display message
    elif guess < random_number:
        clear()
        guesses += 1
        print(f'{player}, {guess} is too low. Guesses: {guesses}')
    # If guess greater than random number increase counter by 1 and display message
    elif guess > random_number:
        clear()
        guesses += 1
        print(f'{player}, {guess} is too high. Guesses: {guesses}')
    
    # Guessed the number increase counter by 1 and display message
    # Ask player if they want to play again
    else:
        clear()
        guesses += 1
        print(f'Congradulation {player}! You guessed the number {random_number} in {guesses} guesses.')
        guess_list.append(guesses)
        play_again = input(f'{player}, would you like to play again? (y/n)')
        # Player wants to play again. Reset counter to 0 and get new random number
        if play_again.lower().startswith('y'):
            clear()
            guesses = 0
            random_number = number()
        else:
            print(goodbye(player))
            break
 
Last edited:
This is the first error that came up when running your code.
while wanna_play.lower() == "yes": NameError: name 'wanna_play' is not defined

Try to fix this error first
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom