菜鸟关于Python的问题,求解答

一个很简单的猜数游戏
import random

secret = random.randint(1,100)
guess = 0
tries = 0

print "AHOY! I am the Dread Prirate Roberts, and I have a secret!"
print "It is a number from 1 yo 99. I will give you 6 tries. "

while guess != secret and tries < 6:
guess = input("what is 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
为什么每次运行结果都是
AHOY! I am the Dread Prirate Roberts, and I have a secret!
It is a number from 1 yo 99. I will give you 6 tries.
what is yer guess? 1
Too low, ye scurvy dog!
No more guesses! Better luck next time, matey!
The secret number was 47
what is yer guess? 2
Too low, ye scurvy dog!
No more guesses! Better luck next time, matey!
The secret number was 47
what is yer guess? 3
Too low, ye scurvy dog!
No more guesses! Better luck next time, matey!
The secret number was 47
what is yer guess? 47
Avast! Ye got it! Found my secret, ye did!
我用的版本是2.7.5

你将
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
改成
if guess == secret:
print "Avast! Ye got it! Found my secret, ye did!"
elif tries >= 6:
print "No more guesses! Better luck next time, matey!"
print "The secret number was", secret
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-06-18
检查一下最后两个print前面的空格吧,估计是有个是Tab,有个是空格
第2个回答  2013-06-18
- -你想问什么?是要每行注释?程序目测貌似没问题啊.
相似回答