Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.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_Variables_Input_Integer_Rating - Fatal编程技术网

python编程的这段代码有什么问题?

python编程的这段代码有什么问题?,python,variables,input,integer,rating,Python,Variables,Input,Integer,Rating,我是python新手 以下代码应将整数读入投票评级: `rating = input('Enter an integer rating between 1 and 10')` 我的疑问是:上面代码的问题是它允许任何值都没有错误。如何插入错误消息 您可以尝试将字符串解析为整数,如果不能,则相应地打印,但如果可以,且整数介于1和10之间,则相应地决定 def check_int(s): is_int = False try: int(s) is_i

我是python新手

以下代码应将整数读入投票评级:

`rating = input('Enter an integer rating between 1 and 10')`

我的疑问是:上面代码的问题是它允许任何值都没有错误。如何插入错误消息

您可以尝试将字符串解析为整数,如果不能,则相应地打印,但如果可以,且整数介于1和10之间,则相应地决定

def check_int(s):

    is_int = False
    try:
        int(s)
        is_int = True
    except:
        pass

    return is_int

rating = input('Enter an integer rating between 1 and 10>>')
#If string can be converted to integer
if check_int(rating):
    #Convert it to an integer and compare ranges
    r = int(rating)
    if 1<=r<=10:
        print('Integer is', r)
    else:
        print('Integer is not between 1 and 10')
#Else print error
else:
    print('Not an integer')

您可以使用如下函数: 虽然输入不正确,但我们要求新的输入。我们还用cast检查输入是否为数字。如果它不是一个数字,那么将引发一个异常,我们将在
try。。。捕捉

def getInputVal():
    # Boolean equal to false while the input isn't correct
    correct_answer = False
    while (not correct_answer):

        # Read the input (string)
        val = input('Enter an integer rating between 1 and 10: ')

        try:                                        # Try to cast the string as an integer
            val_int = int(val)
            if (val_int >= 1 and val_int <= 10):    # If the value is in the right interval
                correct_answer = True               # We go out of the loop
                print("Well done, your value is: ", val_int)           # We display the value
        except:                                     # If the cast raise an error
            print("A number is expected")           # An "error" message is shwon
def getInputVal():
#输入不正确时布尔值等于false
正确答案=错误
而(回答不正确):
#读取输入(字符串)
val=input('输入一个介于1和10之间的整数评级:')
try:#尝试将字符串转换为整数
val_int=int(val)

if(val_int>=1和val_int
input()
始终返回字符串,它从不检查它是整数、浮点还是字符串。您必须自己检查它。首先您可以将字符串转换为整数
value=int(rating)
以检查它是否为整数,然后使用
1@furas
input()检查范围
在python 3中始终返回字符串。在python 2中,它尝试
eval()
传递给它的表达式。如果用户需要python 2中的字符串,则需要使用
raw\u input()
您可以尝试将str解析为int,然后做出相应的决定@user11052359@MatthewBarlowe我知道这一点,但我已经有四五年没有使用Python2了。有人使用过它吗?
def getInputVal():
    # Boolean equal to false while the input isn't correct
    correct_answer = False
    while (not correct_answer):

        # Read the input (string)
        val = input('Enter an integer rating between 1 and 10: ')

        try:                                        # Try to cast the string as an integer
            val_int = int(val)
            if (val_int >= 1 and val_int <= 10):    # If the value is in the right interval
                correct_answer = True               # We go out of the loop
                print("Well done, your value is: ", val_int)           # We display the value
        except:                                     # If the cast raise an error
            print("A number is expected")           # An "error" message is shwon