【发布时间】:2014-09-26 17:01:22
【问题描述】:
我对编码还很陌生,遇到了一个我无法弄清楚或找不到答案的问题。
基本上每次用户在 raw_input 中输入 yes 时,它都会吐出 'if' 字符串,但不会排除 'else' 字符串。
我假设它是因为延迟正在干扰,我没有正确设置它,因为在代码中它会运行(如果、For、Else),也许 For 阻碍了代码,我不知道。将不胜感激一些帮助! :)
import sys
import time
string = 'Hello comrade, Welcome!\n'
for char in string:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(.03)
time.sleep(1)
x=raw_input('Are you ready to enter the fascinating Mists of Verocia? ')
if x == 'yes':
string = "Verocia was a mystical land located just south of Aborne"
for char in string:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(.03)
else:
print ('Please restart program whenever you are ready!')
【问题讨论】:
-
你的缩进是错误的,因为它目前写的
if没有else-else属于for循环,只要不使用break就会执行在循环内部。 -
Python 是缩进敏感的!
标签: python if-statement for-else