新人救助 一个Python 的猜数游戏

baidu_33833677 2016-01-23 09:39:15
import random

secret = random.randint(1, 100)
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 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,marey!"
print "The secret number was", secret

然后它就提示错误了

HOY! I'm the Dread Pirate Roberts,and I have a secret!
It is a number from 1 to 99. I'll give you 6 tries.
What's yer guess?

Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\NumGuess.py", line 11, in <module>
guess = input("What's yer guess? ")
File "<string>", line 0

^
SyntaxError: unexpected EOF while parsing
>>>
我的版本是2.7.9 书里的版本是2.5 难道是版本问题?
...全文
297 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiamin 2016-02-04
  • 打赏
  • 举报
回复
类型不对的问题
xiamin 2016-02-04
  • 打赏
  • 举报
回复
import random secret = random.randint(1, 100) 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 yer guess? ") guess =int(guess) if guess < secret: print ("Too low, ye scurvy dog!") elif guess > secret: print ("Too high, landlubber!") else: print ("Avast! Ye got it! Found my secret,ye did!") tries = tries + 1 if tries == 6: print ("No more guesses! Better luck next time,marey!") print ("The secret number was", secret)
coderCold 2016-01-26
  • 打赏
  • 举报
回复
跑了一下你的程序没有问题,我的也是2.7 但是你这个程序逻辑写的有问题吧 import random secret = random.randint(1, 100) 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 yer guess? ") if guess < secret: print "Too low, ye scurvy dog!" elif guess > secret: print "Too high, landlubber!" else: print "Avast! Ye got it! Found my secret,ye did!" tries = tries + 1 if tries == 6: print "No more guesses! Better luck next time,marey!" print "The secret number was", secret
大熊猫侯佩 2016-01-24
  • 打赏
  • 举报
回复
文件结尾没有处理

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧