I'm trying to make a guess the number game in python. However, in the again() function I cannot seem to get the if statement to run.
When I don't use the again function and copy and paste all the code in the respective positions, it works fine. However when I use the again function, the "play again?" question is asked but the if statement is ignored, causing the while loop to continue endlessly. I have tried using the global function but on the second time I input a guess, it comes up with TypeError: 'str' object is not callable .
import random
def guess():
global num
num = random.randint(1,3)
guessat = input("Out of 1 to 3, which number do you guess? ")
return(guessat)
def again():
global again
again = input("Play again? ")
if again in ["y","yes"]:
guessing = True
else:
guessing = False
print("Welcome to Guess the Number!")
guessing = True
while guessing:
guessy = guess()
guessy = int(guessy)
if guessy == num:
print("You got it right!")
again()
elif guessy != num:
print("Wrong number!")
again()
quit()
When I input "no" or anything else for the question "Play Again?" I expect the program to exit the while loop and quit.