我对编码很新,我遇到了一个我无法弄清楚或找不到答案的问题。
基本上每当用户在raw_input中输入yes时,它会吐出'if'字符串,但不会排除'else'字符串。
我假设它是因为延迟是干扰而我没有正确设置它,因为在代码中(If,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!')
答案 0 :(得分:2)
请注意缩进。我认为for循环应该在if语句中。
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!')
答案 1 :(得分:0)
您必须缩进 for 循环。 Python中的循环有 else 子句 - 它在循环运行时执行,没有中断发布
答案 2 :(得分:0)
正确缩进for循环,您将获得结果。
import sys
import time
strWelcome = 'Hello comrade, Welcome!\n'
for char in strWelcome :
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':
str1 = "Verocia was a mystical land located just south of Aborne"
for char in str1:
sys.stdout.write(char)
sys.stdout.flush()
time.sleep(.03)
else:
print ('Please restart program whenever you are ready!')
答案 3 :(得分:0)
您的代码中存在缩进问题。它应该是:
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!')
答案 4 :(得分:0)
在您的示例中,else条件连接到for语句。 else套件在for之后执行,但前提是for for normal(不是中断)。