-1

私はコーディングにかなり慣れていないので、理解できない、または答えを見つけることができない問題に遭遇しました。

基本的に、ユーザーが 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!')
4

5 に答える 5

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!')
于 2014-08-04T09:00:37.347 に答える
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!')
于 2014-08-04T09:03:05.903 に答える
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!')
于 2014-08-04T09:03:41.853 に答える
0

あなたの例では、else 条件は for ステートメントに接続されています。else スイートは for の後に実行されますが、for が正常に (ブレークによってではなく) 終了した場合にのみ実行されます。

于 2014-08-04T09:03:58.100 に答える
0

forループをインデントする必要があります。Python のループにはelse句があります -ブレークが発行されずに、ループが実行されると実行されます

于 2014-08-04T09:01:15.590 に答える