如果输入不是整数,则请求用户重新输入

时间:2019-06-01 14:38:43

标签: python valueerror

While True:
    n=turtle.textinput(“”,”write your grade”)
    grade = int(n)

    while int(n) != grade :
        n=turtle.textinput(“”,”use only number”)
        grade = int(n)

    If grade <=100 and grade>=95 :
        print(“A+“)
    else :
        print(“F”)

我遇到价值错误。

我想了解情况<-如果用户给我字面上的答案,我想说“只有数字”。

2 个答案:

答案 0 :(得分:0)

我想你是想试试这个

def enforce_int(x):
    try:
        _ = int(x)
        return True
    except ValueError as _:
        return False

While True: # why is this here though?
    n=turtle.textinput(“”,”write your grade”)

    while not enforce_int(n):
        n=turtle.textinput(“”,”use only number”)

    grade = int(n)
    If grade <=100 and grade>=95 :
        print(“A+“)
    else :
        print(“F”)

答案 1 :(得分:0)

下次请以ASCII格式设置代码。

while True:
    n = input("write your grade: ")
    while True:
        try:
            grade = int(n)
            break
        except:
            n=input("use only number: ")

    if grade <=100 and grade>=95 :
        print("A+")
    else :
        print("F")