Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.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中将输入限制为仅整数并显示错误消息_Python_Python 3.x_Input - Fatal编程技术网

如何在Python中将输入限制为仅整数并显示错误消息

如何在Python中将输入限制为仅整数并显示错误消息,python,python-3.x,input,Python,Python 3.x,Input,我只是尝试得到两个输入,它们都应该是整数,并给出与它们的比较相关的解释。同时,当输入属于其他值时,我想给他们一条错误消息。但是,当我想通过输入字符串输入来测试错误消息时,我会得到这个错误,而不是创建一个循环来再次显示主要问题: Traceback (most recent call last): File "C:\Users\Green\PycharmProjects\pythonProject6\main.py", line 21, in <module>

我只是尝试得到两个输入,它们都应该是整数,并给出与它们的比较相关的解释。同时,当输入属于其他值时,我想给他们一条错误消息。但是,当我想通过输入字符串输入来测试错误消息时,我会得到这个错误,而不是创建一个循环来再次显示主要问题:

Traceback (most recent call last):
  File "C:\Users\Green\PycharmProjects\pythonProject6\main.py", line 21, in <module>
    comparison()
  File "C:\Users\Green\PycharmProjects\pythonProject6\main.py", line 2, in comparison
    homeSide = int(input("Please enter how many goals did the home side score:"))
ValueError: invalid literal for int() with base 10: 'adasdasdsa'
总之,我怎样才能克服这个问题。提前感谢您的帮助:)

输出:

Please enter how many goals did the home side score:234  
Please enter how many goals did the away side score :asdfasd
Please enter a number!
Please enter how many goals did the away side score :32
Home side got 3 points.

除了第一个输入之外,添加try和

相反,将输入添加到循环并将其中断,可以执行类似的操作,这看起来更整洁

def comparison():
    
    homeSide = None
    awaySide = None
    
    while homeSide is None:
        try:
            homeSide = int(input("Please enter how many goals did the home side score:"))
        except ValueError:
            print("Please enter a number")
            
    while awaySide is None:
        try:
            awaySide = int(input('Please enter how many goals did the away side score :'))
        except ValueError:
            print("Please enter a number!")
            

    if homeSide > awaySide:
        print("Home side got 3 points.")

    elif homeSide == awaySide:
        print("Both sides got 1 point.")

    else:
        print("Home side could not get any point.")

comparison()


Please enter how many goals did the home side score:i
Please enter a number
Please enter how many goals did the home side score:90
Please enter how many goals did the away side score :as
Please enter a number!
Please enter how many goals did the away side score :1
Home side got 3 points.

这是基本的想法

while True:
    try:
        x = int(input())
        break
    except ValueError:
        print("Error: invalid type!")

您还可以使用以下命令验证变量:isInstance(int,variable)

例如:

homeSide = input("Please enter how many goals did the home side score:")

if not isInstance (int, homeSide):
   print(f"{homeside} is not a number. Please enter a number using 1-10")

homeSide=…
放在try-except中。在请求“客场侧”时,您已经在处理ValueError,因此只需对“主场侧”执行相同操作。相关:当homeSide为None时,最好将其更改为
,否则
0
将使您处于循环中。@MikeScotty好球。!固定的。
while True:
    try:
        x = int(input())
        break
    except ValueError:
        print("Error: invalid type!")
homeSide = input("Please enter how many goals did the home side score:")

if not isInstance (int, homeSide):
   print(f"{homeside} is not a number. Please enter a number using 1-10")