【问题标题】:how to use (while) and (try) statements to handle errors of user inputs from one of three choices only如何使用 (while) 和 (try) 语句仅处理来自三个选项之一的用户输入错误
【发布时间】:2020-12-10 14:03:07
【问题描述】:

我是 Python 的初学者,我需要编写一个交互式代码,在其中我问用户你喜欢 x、y 还是 z 中的哪一个? 我想使用 (while) 循环和 (try) 语句来做到这一点。

我尝试了以下方法:

q1 = input('Would like to see data of Washington, Chicago or New York? \n')

while q1 == 'Washington'or =='Chicago' or == 'New York'
    try:
        print()
        break
    except:
        print('invalid input, please select a name of a city!')

【问题讨论】:

  • 这不是比较和逻辑运算符的工作方式。您需要在所有三个比较中使用q1。您还可以反转逻辑。您想在此条件为 not true 时循环。
  • 无论你在 try 块中的什么,通常都会检查,如果有任何错误,然后你在 except 块中写入相应的错误消息
  • 其实这段代码还有一些问题。您似乎仍然缺乏有关编程如何工作的一些基本概念。你应该找一位老师或阅读一本好书或在线教程。 Stack Overflow 并不是要取代它。
  • @mkrieger1 我不同意你的观点。条条大路通罗马。有不同的学习方式。

标签: python error-handling while-loop try-catch user-input


【解决方案1】:

你试过了吗?

q1 = input('Would like to see data of Washington, Chicago or New York? \n')

while q1 == 'Washington' or q1 =='Chicago' or q1 == 'New York':
    try:
        print()
        break
    except:
        print('invalid input, please select a name of a city!')

您需要为每个条件语句重复q1...

【讨论】:

    【解决方案2】:

    试试这样的:

    while true:
        q1 = input('Would like to see data of Washington, Chicago or New York? \n')
        if q1 not in [ 'Washington', 'Chicago', 'New York' ]:
            print('invalid input, please select a name of a city!')
        else:
            break
    

    【讨论】:

      【解决方案3】:

      要限制用户在某些选择中的输入,最好将这些选择放在一个列表中,然后将输入与它们进行如下比较:

      choices = ['Washington', 'Chicago', 'New York']
      q1 = input("Would like to see data of Washington, Chicago or New York? \n")
      while q1 not in choices:
          print('invalid input, please select a name of a city!')
          q1 = input()
      

      所以稍后如果您想添加更多选项,您可以通过修改选项变量轻松完成。 此代码将在 while 循环中阻塞,直到用户输入成为选项之一。但是,用户输入必须与选项中的选项完全一致(区分大小写)(即,芝加哥不起作用,它应该是芝加哥,大写“c”)。

      我的建议(如果您不介意确切的区分大小写的名称)是这样选择所有小写字母:

      choices = ['washington', 'chicago', 'new york']
      

      然后将用户输入(小写)与以下选项进行比较:

      while q1.lower() not in choices:
          ...
      

      【讨论】:

        【解决方案4】:

        解决方案-1

        如果您想实现持续循环,直到用户输入错误的值,请尝试以下代码。

        while flag:
            q1 = input('Would like to see data of Washington, Chicago or New York? \n')
            try:
                if q1 in [ 'Washington', 'Chicago', 'New York' ]:
                    print('your result is: ' + q1)
                else:
                    flag=0
                    print('Invalid input, please select a name of a city!')
                    break
            except:
                flag=0
                print('Invalid input, please select a name of a city!')
                break
        

        解决方案 - 2

        我们可以通过 if-else 来实现,但是如果你想使用 while 循环,那么试试下面的代码。

        修改后的代码

        q1 = input('Would like to see data of Washington, Chicago or New York? \n')
        flag = 0;
        while (q1 == 'Washington' or q1 =='Chicago' or q1 == 'New York'):
            try:
                flag = 1
                break
            except:
                print('invalid input, please select a name of a city!')
                
        if(flag):
            print('your result is: ' + q1)
        else:
            print('Invalid input, please select a name of a city!')
        
        

        请试试这个代码:

        您的代码是正确的,但只需要在 while 条件下为所有情况添加 q1

        q1 = input('Would like to see data of Washington, Chicago or New York? \n')
        
        while (q1 == 'Washington' or q1 =='Chicago' or q1 == 'New York'):
            try:
                print('your result is: ' + q1)
                break
            except:
                print('invalid input, please select a name of a city!')
        

        【讨论】:

        • 我试过了,当我输入一个无效的值时,它会正常继续并且不显示消息
        • 在这种情况下,我们需要使用 if-else 进行修改。试试这个代码:
        猜你喜欢
        • 2022-07-09
        • 2022-01-24
        • 2019-07-27
        • 1970-01-01
        • 2013-11-02
        • 2021-11-20
        • 1970-01-01
        • 1970-01-01
        • 2019-10-23
        相关资源
        最近更新 更多