'>'在我猜测的数字游戏中,“str"和“int"的实例之间不支持 [英] '>' not supported between instances of 'str' and 'int' in my guess the number game
本文介绍了'>'在我猜测的数字游戏中,“str"和“int"的实例之间不支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的猜测中,数字程序给了我错误:main 中第 26 行的str"和int"实例之间不支持>".如果有人帮助我,我将不胜感激,谢谢.
In my guess the number program it gives me the error: '>' not supported between instances of 'str' and 'int' on line 26 in main. I would be grateful if someone helped me with this, Thank you.
import random
guesses = 0
def higher(guesses):
print("Lower")
gueeses += 1
def lower(guesses):
print("Higher")
guesses += 1
def correct(guesses):
print("You got it correct!")
print("It was {0}".format(number))
guesses += 1
def _main_(guesses):
print("Welcome to guess the number")
number = random.randint(1, 100)
while True:
guess = input("Guess a number: ")
if guess > number:
lower(guesses)
elif guess < number:
higher(guesses)
elif guess == number:
correct(guesses)
while True:
answer = input("Would you like to play again? Y or N: ")
if answer == "Y":
break
elif answer == "N":
exit()
else:
exit()
_main_(guesses)
推荐答案
默认情况下,input
返回一个字符串.您需要将输入转换为数字类型,在本例中为整数.
By default, input
returns a string. You need to convert your input to a numeric type, in this case integer.
替换:
guess = input("Guess a number: ")
与:
guess = int(input("Guess a number: "))
您可能还希望验证用户输入以确保您确实获得了有效的整数.为此,您应该看到 询问用户输入直到他们给出有效的回应.
You may also wish to validate user input to ensure that you have actually been provided with a valid integer. For this you should see Asking the user for input until they give a valid response.
这篇关于'>'在我猜测的数字游戏中,“str"和“int"的实例之间不支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文