| 网站首页 | 业界新闻 | 小组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 221 人关注过本帖
标题:请教:Python入门-Number Guessing Game 错误
只看楼主 加入收藏
dan2019
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2019-11-20
结帖率:50%
  已结贴   问题点数:20  回复次数:4   
请教:Python入门-Number Guessing Game 错误
刚刚开始入门Python,照着书录入,是一个Number Guessing Game,但还是遇到了错误。书里的程序是Python2.7.3下运行的,我是在Python3.7.3运行的,不知道有没有关系。以下是我按照书本录入的code:
import random
secret = random.randint(1,99)
guess = 0
tries = 0
print ("AHOY! I'm the Dread 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 your 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 questions! Better luck next time, matey!")
    print ("The secret number was, secret")

运行之后,有一个错误提示:
Traceback (most recent call last):
  File "D:/Python/Python Practice/Number-guessing Game.py", line 9, in <module>
    if guess < secret:
TypeError: '<' not supported between instances of 'str' and 'int'


请教各位:该怎样修改code?非常感谢!
搜索更多相关主题的帖子: 错误 Python print 入门 运行 
2019-11-20 10:00
fall_bernana
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:4
帖 子:72
专家分:582
注 册:2019-8-16
  得分:10 
以下是引用dan2019在2019-11-20 10:00:04的发言:

刚刚开始入门Python,照着书录入,是一个Number Guessing Game,但还是遇到了错误。书里的程序是Python2.7.3下运行的,我是在Python3.7.3运行的,不知道有没有关系。以下是我按照书本录入的code:
import random
secret = random.randint(1,99)
guess = 0
tries = 0
print ("AHOY! I'm the Dread 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 your 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 questions! Better luck next time, matey!")
    print ("The secret number was, secret")

运行之后,有一个错误提示:
Traceback (most recent call last):
  File "D:/Python/Python Practice/Number-guessing Game.py", line 9, in <module>
    if guess < secret:
TypeError: '<' not supported between instances of 'str' and 'int'

请教各位:该怎样修改code?非常感谢!

返回的错误表示在第9行 不能在字符串和整数之间做<操作. 你需要int(str)后再做< 比较操作
2019-11-20 11:59
dan2019
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2019-11-20
  得分:0 
回复 2楼 fall_bernana
您能再具体一些吗?我是指,具体怎样修改第9行呢?谢谢!
2019-11-20 13:27
fall_bernana
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:4
帖 子:72
专家分:582
注 册:2019-8-16
  得分:10 
回复 3楼 dan2019
guess = int(input ("what's your guess?"))
2019-11-20 14:06
dan2019
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2019-11-20
  得分:0 
回复 4楼 fall_bernana
太感谢了!!!
2019-11-21 11:07
快速回复:请教:Python入门-Number Guessing Game 错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.186855 second(s), 8 queries.
Copyright©2004-2019, BCCN.NET, All Rights Reserved