Python 类型错误:'<';在';str';和';int';
我正在使用Python 类型错误:'<';在';str';和';int';,python,python-3.x,Python,Python 3.x,我正在使用python3创建一个文本游戏 我的代码: import random secret = random.randint(1,99) guess = 0 tries = 0 print (" AHOY! I'm the Dead Pirate Roberts, and I have a secret!") print ("It is a number from 1 to 99. I'll give you 6 tries.") while guess != secret and trie
python3
创建一个文本游戏
我的代码:
import random
secret = random.randint(1,99)
guess = 0
tries = 0
print (" AHOY! I'm the Dead Pirate Roberts, and I have a secret!")
print ("It is a number from 1 to 99. I'll give you 6 tries.")
while guess != secret and tries < 6:
guess = input ("What's yer guess?")
if guess < secret:
print ("Too low, ye scurvy dog!")
elif guess > secret:
print ("Too high, landlubber!")
tries = tries + 1
if guess == secret:
print ("Avast! Ye got it ! Found my secret, ye did!")
else:
print ("No more guesses! Better luck next time, matey!")
print ("The secret number was "), secret
随机导入
secret=random.randint(1,99)
猜测=0
尝试=0
打印(“喂!我是死去的海盗罗伯茨,我有一个秘密!”)
打印(“这是一个从1到99的数字。我会给你6次尝试。”)
猜猜看!=秘密和尝试<6:
猜测=输入(“你的猜测是什么?”)
如果猜测<秘密:
打印(“太低了,你这个坏蛋!”
elif guess>秘密:
打印(“太高了,landubber!”)
尝试=尝试+1
如果猜测==秘密:
打印(“阿瓦斯特!你找到了!找到了我的秘密,你做到了!”)
其他:
打印(“别再猜了!祝你下次好运,伙计!”)
打印(“机密号码”),机密
跑完后,我得到
TypeError:'Python 3.x'input()
函数返回默认值int
。要获取类型为int
的对象,需要显式地键入cast:
guess = int(input ("What's yer guess?"))
当前,guess
是字符串变量,secret
是int,因此您不能使用运算符的try if int(guess)import random
secret = random.randint(1,99)
guess = 0
tries = 0
print (" AHOY! I'm the Dead Pirate Roberts, and I have a secret!")
print ("It is a number from 1 to 99. I'll give you 6 tries.")
while guess != secret and tries < 6:
guess = int(input ("What's yer guess?"))
if guess < secret:
print ("Too low, ye scurvy dog!")
elif guess > secret:
print ("Too high, landlubber!")
tries = tries + 1
if guess == secret:
print ("Avast! Ye got it ! Found my secret, ye did!")
else:
print ("No more guesses! Better luck next time, matey!")
print ("The secret number was "), secret