Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何不断地重复;如果;s_Python_If Statement - Fatal编程技术网

Python 我如何不断地重复;如果;s

Python 我如何不断地重复;如果;s,python,if-statement,Python,If Statement,我希望我的if函数在遇到else时在python上不断重复。我该怎么做?我添加了一个代码示例 if selection1 == "C": print("Ok") elif selection1 == "E": print("Ok") elif selection1 == "Q": print("Ok...") quit() else: selection1 == print("The character you entered has not been recogn

我希望我的if函数在遇到else时在python上不断重复。我该怎么做?我添加了一个代码示例

if selection1 == "C": 
  print("Ok") 
elif selection1 == "E": 
  print("Ok") 
elif selection1 == "Q": 
  print("Ok...") quit() 
else: 
  selection1 == print("The character you entered has not been recognised, please try again.")

我不知道你是不是这个意思,但是这个程序完全按照你的问题所问的去做

while True:
  selection1 = input("Enter Character\n")
  if selection1 == "C": 
    print("Ok") 
  elif selection1 == "E": 
    print("Ok") 
  elif selection1 == "Q": 
    print("Ok...") 
    break 
  else: 
    selection1 == print("The character you entered has not been recognised, please try again.")
程序接收字符作为输入,并用硬编码字符进行检查。如果不匹配,它将要求用户重复,直到字母
Q
匹配为止。输出可以是

Enter Character
C
Ok
Enter Character
E
Ok
Enter Character
v
The character you entered has not been recognised, please try again.
Enter Character
Q
Ok...

这里有两种可能的方法

while True:   # i.e. loop until I tell you to break

    selection1 = GetSelectionFromSomewhere()

    if selection1 == 'C':
        print('Okay...')
        break
    elif selection1 == 'E':
        print('Okay...')
        break
    elif selection1 == 'Q':
        print('Okay...')
        quit()
    else:
        Complain()
一些纯粹主义者不喜欢
而真正的
循环,因为他们一眼就看不出循环条件是什么。下面是另一个列表,它的优点是将
break
语句和其他内务处理排除在
if
级联之外,这样您就可以集中精力进行必要的操作:

satisfied = False
while not satisfied:
    selection1 = GetSelectionFromSomewhere()
    satisfied = True
    if selection1 == 'C':
        print('Okay...')
    elif selection1 == 'E':
        print('Okay...')
    elif selection1 == 'Q':
        print('Okay...')
        quit()
    else:
        Complain()
        satisfied = False
但是你已经从以前的帖子中找到了答案。这只是另一种选择。

,-也有漂亮的图片
while True:   # i.e. loop until I tell you to break

    selection1 = GetSelectionFromSomewhere()

    if selection1 == 'C':
        print('Okay...')
        break
    elif selection1 == 'E':
        print('Okay...')
        break
    elif selection1 == 'Q':
        print('Okay...')
        quit()
    else:
        Complain()
satisfied = False
while not satisfied:
    selection1 = GetSelectionFromSomewhere()
    satisfied = True
    if selection1 == 'C':
        print('Okay...')
    elif selection1 == 'E':
        print('Okay...')
    elif selection1 == 'Q':
        print('Okay...')
        quit()
    else:
        Complain()
        satisfied = False
characters = {'C','E','Q'}
def check():
    ch=raw_input("type a letter: ")
    if ch == q:
        quit()
    print "ok" if ch in characters else check()
    check()