'>'我猜数字游戏中'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屋!
查看全文