菜鸟关于Python的问题,求解答
一个很简单的猜数游戏importrandomsecret=random.randint(1,100)guess=0tries=0print"AHOY!IamtheDrea...
一个很简单的猜数游戏
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 展开
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 展开
3个回答
展开全部
你将
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
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |