2016-04-14 6 views
0

私は自分自身でパイソンを教えようとしており、特定の部分で何をすべきかについてはあまり考えていません。私が何を起こそうとしているのは、答えがNOの場合、ユーザーが最終的にYESと答えるまで質問を繰り返すだけで、残りの「ゲーム」が起こるということです。if文をループしますか?

import random 
import time 

print("Think of a number from one to twenty. I will guess it.") 
now = time.time() 
questionTime = now + 2 
while time.time() < questionTime: 
    pass 
question = input(" Yes or No? ") 
if question.upper() == "NO": 

elif question.upper() == "YES": 
+0

予想外の動作をしているコードで現在起こっていることを説明できますか?受け取っているエラーメッセージを含めてください。 – idjaw

+2

チュートリアルまたは2つのhttp://anandology.com/python-practice-book/をお読みください –

+1

[公式のPythonチュートリアル](https://docs.python.org/3.4/tutorial/index.html)は良いですあまりにも。 – TigerhawkT3

答えて

2

、ドキュメントを読んだりチュートリアルをナビゲートすることはあなたのために素晴らしいことです!私はCodeCademyをお勧めします。

while True: 
    doContinue = input('Would you like to start Y/N ?') 
    if doContinue.lower() == 'y' or doContinue.lower() == 'yes': 
     break 
+1

と簡潔さのために1)。 *大文字の文字については、str.lower()は大文字と小文字の比較には問題ありませんが、他の種類の文字でも*常に*動作するので、 'str.casefold() 'を使うことをお勧めします。 – Signal

+1

私は以前CodeCademyを見てきました。 –

0

はこれを試してみてください:コメントの状態と同様に

import random 
import time 

print("Think of a number from one to twenty. I will guess it.") 
now = time.time() 
questionTime = now + 2 
while time.time() < questionTime: 
    pass 

while 1: 
    question = input(" Yes or No? ") 
    if question.upper() == "YES": 
     break 
+1

なぜwhile time.time() Signal

0

TheLazyScripterのようなより単純な例を紹介していますが、このチュートリアルの内容はどうでしょうか?

import random 
import time 

print("Think of a number from one to twenty. I will guess it.") 
now = time.time() 
questionTime = now + 2 
while time.time() < questionTime: # Very CPU-intensive. Maybe use time.sleep(2)? 
    pass 
while True: 
    number = random.randint(1, 20) 
    question = input("Is it " + str(number) + "? - Yes or No? ") 
    if question.upper() == "NO": 
     continue 
    elif question.upper() == "YES": 
     break 
関連する問題