当我不使用again函数并在相应位置复制和粘贴所有代码时,它可以正常工作。不过,当我使用“再次播放”功能时,“再次播放?”问题被提出,但if语句被忽略,导致while循环无休止地继续。我试过使用global函数,但在第二次输入guess时,它出现了TypeError:“str”对象不可调用。
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()