如何在不使用break的情况下重启程序?

时间:2016-06-12 04:17:21

标签: python if-statement

answer = input('how are you')
if answer == 'good':
    print('glad to hear it')
if answer == 'what?':
    print('how are you?')

不使用break,如果用户输入'what?',如何再次从头开始?我如何只使用变量和循环?

3 个答案:

答案 0 :(得分:1)

'''它一直循环直到答案是好的。没有使用任何标记'''

 answer='#'
 while(answer != 'good'):
     answer = input('how are you\n') 
     if answer == 'good':
         print('glad to hear it')

答案 1 :(得分:0)

这应该可以正常运作:

good = False

while not good:
    answer = input('how are you?')
    if answer == 'what?':
        continue

    if answer == 'good':
        good = True
        print('glad to hear it')

当变量good变为True时,循环停止。 continue跳到循环的下一次迭代,但是,没有必要。离开它会向读者显示'what?'是预期的输入。

现在,你说你不能使用break,但是,如果可以的话,它会是这样的:

while True:
    answer = input('how are you?')
    if answer == 'what?':
        continue

    if answer == 'good':
        print('glad to hear it')
        break

答案 2 :(得分:0)

你不需要任何复杂的事情来实现这一点。

input = '' #nothing in input
while input != 'good': #true the first time
  input = raw_input('how are you?') #assign user input to input
if input == 'good': #if it's good print message
  print('glad to hear it')

input = 'what?' #what? in input
while input == 'what?': #true the first time
  input = raw_input('how are you?') #assign user input to input
if input == 'good': #if it's good print message
  print('glad to hear it')
else:
  print('too bad')

第一种情况,如果您期望good,则第二种情况,如果有任何回复,则what?除外。