循环if语句?

时间:2016-04-14 00:09:54

标签: python loops

我正在尝试自学自己的python,我对于在某些部分做什么没有太多的想法。我想要得到的是,如果答案是否定的,它只是循环问题,直到用户最终回答“是”,然后“游戏”的其余部分将会发生。

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":

3 个答案:

答案 0 :(得分:2)

与评论状态一样,阅读文档或导航教程对您来说非常有用!我推荐CodeCademy

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

答案 1 :(得分: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

答案 2 :(得分: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