我正在尝试从用户输入中构建一个列表,直到他们说“是”为止。我已经按照下面的说明进行了操作,但是很快意识到我将不得不继续写一行代码多次,直到允许用户输入为止。但是,其想法是让用户构建一个数字列表,直到他们对数字列表感到厌烦为止,以便稍后在程序中引用它。因此,基本上我想引用第6行,直到遇到“是”为止。
play_num = int
play_num = input('Give me a number.')
play_num = int(play_num) + int(1)
choice1 = str
AnnoyList = []
choice1 = input ("You said " + str(play_num) + " right? ")
if (choice1.lower is "no"):
AnnoyList.append(play_num)
play_num = input ("Oh, sorry what was it then? ")
AnnoyList.append(play_num)
if (choice1.lower is "yes")
答案 0 :(得分:0)
查看while
循环。设置一个等于True
的变量并循环直到用户输入no,然后将其设置为False
。
或者,如果您知道要循环多少次,请查看for
循环。
答案 1 :(得分:0)
当用户输入yes
时,使用while循环使用break语句。在这里阅读有关它们的信息:Control Flow Statements
您的代码将如下所示
AnnoyList = []
choice1 = "no"
play_num = input('Give me a number.')
while choice1.lower()!="yes":# notice the parenthesis
play_num = int(play_num) + (1)
choice1 = input ("You said " + str(play_num) + " right? ")
if (choice1.lower() == "no"):
AnnoyList.append(play_num)
play_num = input ("Oh, sorry what was it then? ")