Following Code is not terminating not sure why.
def main():
pass
if __name__ == '__main__':
main()
import random # imports random
def GuessingGame (): # creating def function for GuessingGame
yes = ['y', 'Y', 'Yes', 'yes'] # setting variables
rng = random.Random ()
numbertoguess = rng.randrange (1,10) # setting random number between 1 and 10
correctguesses = 0
winpercent = 0
attempts = 0
name = input('What is your name? ') # asks user for name and introduces the game
print('Nice to meet you', name, '! I am going to pick a random number and you will have 3 tries to guess it.'
'Good luck!')
while attempts < 3: # creates a function
guess = int(input('Guess the number I have chosen between 1 and 10. '))
attempts += 1
if guess == numbertoguess: # tells user if guess is right
print('Great guess, youre right', name, '!')
correctguesses += 1 # adds on to correct guesses
winpercent = float((correctguesses*100)/attempts)# determines percent of correct answers
print ('You have' , correctguesses, 'correct guesses!')
print ('You are right', winpercent, 'of the time.')
elif guess > numbertoguess: # tells if guess is too high
print('The number is lower than', guess, "!")
elif guess < numbertoguess: # tells if guess is too low
print('The number is higher than', guess, "!")
else: # tells when you have lost
print('Wrong, the number was', numbertoguess, '!')
print ('You have' , correctguesses, 'correct guesses!')
print ('You are right', winpercent, 'of the time.')
gameover = input("Do you want to play again? Yes or No? ")
while gameover is 'Yes' or 'yes': GuessingGame () #oportunity to play again
else: exit() # ends games
print ('The program will now terminate. Goodbye.')
GuessingGame () # calls game
I believe your problem is in the line
while gameover is 'Yes' or 'yes': GuessingGame ()
If the user enters yes to if they want to play the game again, you are calling the function over and over again, which is known as recursion. If I'm correct, this will reset attempts to 0, thus never exiting your while loop. I am not sure if this is what you want, but instead of calling GuessingGame(), writingwhile gameover is 'Yes' or 'yes': continue
will actually go back to the top of the while loop.Also, I'm not quite sure of what the importance of defining a main() function is, when the body of the function is just pass. Instead, you should write your GuessinGame() above if name == 'main', and then have your
while
loop below if name == 'main'