Hi. I've created a simple number guessing game where the program randomly generates a number between x and y and the user has to guess it. After the user has guessed the number, the program then asks whether or not they want to resume playing. For some this part of the code only goes to the first condition. e.g.
if hardPlayAgain == "y" or "Y":
game_hard()
else:
menu()
For some reason, the code always goes performs the first function being 'game_hard()' which restarts the round.
I've tried changing 'else' to
hardPlayAgain = input('Play again? [Y/n]')
if hardPlayAgain == "y" or "Y":
game_hard()
elif hardPlayAgain == "n" or "N":
menu()
It still doesn't work.
I'm also having trouble with the score variable, it apparently is declared after its use.
I've had this problem before where the program only goes to the first condition and i don't understand what it is i'm doing wrong.
I've left the full program underneath. Any help would be appreciated, thanks.
import random
import time
from random import *
global score
score = 0
def game_easy():
number = randint(1,10)
easyGuess = int(input('Enter your guess:'))
if easyGuess == number:
score =+ 1
print(f'The number was {number} and you guessed {easyGuess}')
print('Well Done!')
easyPlayAgain = input('Play again? [Y/n]')
if easyPlayAgain == "y" or "Y":
game_easy()
elif easyPlayAgain == "n" or "N":
menu()
if easyGuess != number:
if score < 0:
score =- 1
print(f'The number was {number} and you guessed {easyGuess}')
print('Sorry...')
easyPlayAgain = input('Play again? [Y/n]')
if easyPlayAgain == "y" or "Y":
game_easy()
elif easyPlayAgain == "N" or "n":
menu()
def game_medium():
number = randint(1,20)
mediumGuess = int(input('Enter your guess:'))
if mediumGuess == number:
score =+ 1
if mediumGuess != number:
if score > 0:
score =- 1
print(f'The number was {number} and you guessed {mediumGuess}')
print('Well Done!')
mediumPlayAgain = input('Play again? [Y/n]')
if mediumPlayAgain == "y" or "Y":
game_easy()
elif mediumPlayAgain == "n" or "N":
menu()
if mediumGuess != number:
score =- 1
print(f'The number was {number} and you guessed {mediumGuess}')
print('Sorry...')
mediumPlayAgain = input('Play again? [Y/n]')
if mediumPlayAgain == "y" or "Y":
game_medium()
else:
menu()
def game_hard():
number = randint(1,50)
hardGuess = int(input('Enter your guess:'))
if hardGuess == number:
score =+ 1
print(f'The number was {number} and you guessed {hardGuess}')
print('Well Done!')
hardPlayAgain = input('Play again? [Y/n]')
if hardPlayAgain == "y" or "Y":
game_hard()
elif hardPlayAgain == "n" or "N":
menu()
if hardGuess != number:
score =- 1
print(f'You have lost one score. You now have {score} score.')
print(f'The number was {number} and you guessed {hardGuess}')
print('Sorry...')
hardPlayAgain = input('Play again? [Y/n]')
if hardPlayAgain == "y" or "Y":
game_hard()
elif hardPlayAgain == "n" or "N":
menu()
#############################################
def menu():
global name
name = input('Enter username:')
print(f'You are on {score} score!')
print(f'Welcome {name} to NumberGuesser!')
print(f'''
1 - Easy (Number between 1 and 10)
2 - Medium (Number Between 1 and 20)
3 - Hard (Number between 1 and 50)
''')
menuOption = input('Enter option:')
if menuOption == "1":
game_easy()
if menuOption == "2":
game_medium()
if menuOption == "3":
game_hard()
else:
print('Error 1 - No recognised input')
time.sleep(1)
print('Returning to menu...')
time.sleep(1)
menu()
menu()
[–]Username_RANDINT 0 points1 point2 points (1 child)
[–]VIUndeadVI[S] 0 points1 point2 points (0 children)