python-3.x 简单程序

zysjyyx4  于 2023-11-20  发布在  Python
关注(0)|答案(4)|浏览(129)

我们得到了一个小程序来调试,我被困在最后一块拼图上。这是一个评分系统程序。
小于0或大于100的数字将导致错误,并要求用户再次输入。
我被这件事卡住了。有人能帮我吗?

# This program converts a numeric grade to an alpha grade

input_bool = False

while not input_bool:
    input_grade = int(input("Please enter the grade you wish to convert (0-100).\n"))
    if (input_grade >= 0 ) or (input_grade <= 100):
        input_bool = True

    output_grade = ""

    if input_grade >= 0 and input_grade <= 34:
        output_grade = "F"

    elif input_grade >= 35 and input_grade <= 39:
        output_grade = "MF"

    elif input_grade >= 40 and input_grade <= 49: 
        output_grade = "D"

    elif input_grade >= 50 and input_grade <= 59: 
        output_grade = "C"

    elif input_grade >= 60 and input_grade <= 69: 
        output_grade = "B"

    elif input_grade >= 70 and input_grade <= 79: 
        output_grade = "A-"
    
    elif input_grade >= 80 and input_grade <= 89: 
        output_grade = "A"
    
    elif input_grade >= 90 and input_grade <= 100:
        output_grade = "A+"

    print(f"{input_grade} = {output_grade}.")

字符串

cl25kdpy

cl25kdpy1#

我想我已经解决了,试着看看这段代码:

def calc():

    again_or_not = input("Do you want to play [Y/N]: ")

    if again_or_not == "n":
        print("Good luck, see you soon")

    elif again_or_not == "y":
            # This program converts a numeric grade to an alpha grade

        input_bool = False

        while not input_bool:
            input_grade = int(input("Please enter the grade you wish to convert (0-100).\n"))
            if (input_grade >= 0 ) or (input_grade <= 100):
                input_bool = True

            output_grade = ""

            if input_grade >= 0 and input_grade <= 34:
                output_grade = "F"

            elif input_grade >= 35 and input_grade <= 39:
                output_grade = "MF"

            elif input_grade >= 40 and input_grade <= 49: 
                output_grade = "D"

            elif input_grade >= 50 and input_grade <= 59: 
                output_grade = "C"

            elif input_grade >= 60 and input_grade <= 69: 
                output_grade = "B"

            elif input_grade >= 70 and input_grade <= 79: 
                output_grade = "A-"
            
            elif input_grade >= 80 and input_grade <= 89: 
                output_grade = "A"
            
            elif input_grade >= 90 and input_grade <= 100:
                output_grade = "A+"

        print(f"{input_grade} = {output_grade}.")
        again_or_not2 = input("Do you want to try again [Y/N]: ")
        if again_or_not2 == "n":
            print("Good luck, see you soon")
        else:
            calc()

calc()

字符串

r7s23pms

r7s23pms2#

代码

if (input_grade >= 0) or (input_grade <= 100):
    input_bool = True

字符串
只检查一个条件是否为真,并传递到下一行。为了检查数字是否在0和100之间,您必须使用and运算符。
你也可以先检查if not input_bool:,然后再检查continue,以避免额外的条件检查,因为它甚至不在0-100之下。

eqqqjvef

eqqqjvef3#

试试这个:

input_bool = False

while not input_bool:
    input_grade = int(input("Please enter the grade you wish to         convert (0-100).\n"))
    if (input_grade >= 0) or (input_grade <= 100):
        input_bool = False

        output_grade = "Error"

    if 0 <= input_grade <= 34:
        output_grade = "F"

    elif 35 <= input_grade <= 39:
        output_grade = "MF"

    elif 40 <= input_grade <= 49:
        output_grade = "D"

    elif 50 <= input_grade <= 59:
        output_grade = "C"

    elif 60 <= input_grade <= 69:
        output_grade = "B"

    elif 70 <= input_grade <= 79:
        output_grade = "A-"

    elif 80 <= input_grade <= 89:
        output_grade = "A"

    elif 90 <= input_grade <= 100:
        output_grade = "A+"

    print(f"{input_grade} = {output_grade}.")

字符串

b0zn9rqh

b0zn9rqh4#

问题出在你的if条件中。你正在使用or运算符,它将始终计算为True,因为任何等级输入都将是>= 0<= 100。要解决这个问题,你应该使用and运算符来确保等级是>= 0<= 100

while not input_bool:
    input_grade = int(input("Please enter the grade you wish to convert (0-100).\n"))
    if (input_grade >= 0 ) and (input_grade <= 100):  # Alternatively if 0 <= input_grade <= 100:
        input_bool = True
    else:
        print("Invalid input. Please enter a grade between 0 and 100.")
        continue

    # Rest of your code

字符串
此更改确保input_bool仅在等级在有效范围内时才设置为Trueelse块处理无效的输入情况,提示用户再次输入等级。

相关问题