我正在为我的基本python类设置游戏,并希望增加在有人在游戏中途键入数字而不是数字时退出的能力。
我已经尝试过重新排列代码,添加不同的while语句,但是我很新,所以可能只是我想念的东西。
这是我想添加退出功能的部分代码。目前,一旦他们正确回答了电话号码,我就可以开始和结束退出。
i = name while i != "quit": print('Hello,',name+'!') # Step 3: input enter a number from 1 to 10 for the variable your_guess your_guess = int(input('Enter a number between 1 and 10.')) # display the number guessed #print("Your number is", your_guess) - redundent, if added after your_guess, # it will show for every guess while num != your_guess: # Step 4: Write an if statement for your_guess is less than num if your_guess < num: print("Your number is", your_guess) print("Your guess is too low.") your_guess = int(input("Guess another number from 1 o 10: ")) elif your_guess > num: print("Your number is", your_guess) print("Your guess is too high") your_guess = int(input("Guess another number from 1 to 10: ")) else: break print("The correct number was",num,"! Congratulations!")
运行时出错:
Traceback (most recent call last):
File "C:\Users\gorde\Downloads\M3Lab1mg-1.2.py", line 38
`enter code here`, in <module>
if your_guess < num:
TypeError: '<' not supported between instances of 'str' and 'int'
我正在为我的基本python类设置游戏,并希望增加在有人在游戏中途键入数字而不是数字时退出的能力。我尝试重新排列代码,...
替换下面的代码行: