stu*_*ped 2 python if-statement
这是我正在阅读的Python书中的一个例子.当我尝试运行程序时出现错误,当我检查错误的代码时elif会以红色突出显示.我在Python 2.5中编程.
import random
secret = random.randint(1, 99)
guess = 0
tries = 0
print "It is a number between 1 and 99. I'll give you six tries. "
while guess != secret and tries < 6:
guess = input("What's your guess? ")
if guess < secret:
print "Too low!"
elif guess > secret:
print "Too high"
tries = tries + 1
if guess == secret:
print "Correct! You found my secret!"
else:
print "No more guesses! Better luck next time!"
print "The secret number was", secret
Run Code Online (Sandbox Code Playgroud)
Python是缩进敏感的.
import random
secret = random.randint(1, 99)
guess = 0
tries = 0
print "It is a number between 1 and 99. I'll give you six tries. "
while guess != secret and tries < 6:
guess = input("What's your guess? ")
if guess < secret:
print "Too low!"
elif guess > secret:
print "Too high"
tries = tries + 1
elif guess == secret:
print "Correct! You found my secret!"
else:
print "No more guesses! Better luck next time!"
print "The secret number was", secret
Run Code Online (Sandbox Code Playgroud)
编辑:我知道这里仍有bug,比如tries = tries + 1应该在代码中的其他地方.但是这个版本至少没有给出语法错误.
| 归档时间: |
|
| 查看次数: |
726 次 |
| 最近记录: |