(Python 3)输出问题

时间:2018-05-09 02:59:24

标签: python function if-statement

我正在尝试创建一个数字级到字母等级的转换器,它接受用户输入,然后吐出等级的字母等级。我的问题是,每当我输入一个数字时,它只吐出字母等级,然后低于吐出的数字#39;你的字母等级是无!'

我的目标是让它到达吐出的地方'你的字母等级是(letter_grade)!"

代码如下:

grade = int(input("Please enter what you made on the test here:"))

def grade_converter(grade):
    if grade >= 94 and grade <= 100:
        return "A"
    elif grade >= 90 and grade < 94:
        return "A-"
    elif grade >=87 and grade < 90:
        return "B+"
    elif grade >= 84 and grade < 87:
        return "B"
    elif grade >= 80 and grade < 84:
        return "B-"
    elif grade >= 77 and grade < 80:
        return "C+"
    elif grade >= 74 and grade < 77:
        return "C"
    elif grade >= 70 and grade < 74:
        return "C-"
    elif grade >= 67 and grade < 70:
        return "D+"
    elif grade >= 64 and grade < 67:
        return "D"
    elif grade >= 61 and grade < 64:
        return "D-"
    else:
        return "F"

letter_grade = print(grade_converter(grade))

print("Your letter grade is " + str(letter_grade) + "!")

3 个答案:

答案 0 :(得分:2)

您需要更改的唯一内容是以下行

letter_grade = print(grade_converter(grade))

letter_grade = grade_converter(grade)

<强>为什么吗
因为印刷品不会返回任何东西。

运行直至用户关闭程序
除了函数

之外,将所有内容修改为以下内容
while(True):
    grade = int(input("Please enter what you made on the test here:"))
    letter_grade = grade_converter(grade)
    print("Your letter grade is " + str(letter_grade) + "!")

答案 1 :(得分:1)

我可以为您的代码提供 2个解决方案。将您的代码更改为此,

datasource = "";
username = "yourUsername";
password = "password";

conn = database(datasource,username,password);

selectquery = 'SELECT image FROM iamgeTable';
data = select(conn,selectquery);

我想提一点的是,您实际上是将值作为字符串返回,因此您不需要中输入再次 str >最后 打印 声明

所以不要这样,

    letter_grade = grade_converter(grade)

只需将其替换为

    letter_grade = grade_converter(grade)

    print("Your letter grade is " + str(letter_grade) + "!")

答案 2 :(得分:-1)

while True:
    grade = int(input("Please enter what you made on the test here:"))
    if grade != -1:
        def grade_converter(grade):
            if grade >= 94 and grade <= 100:
                return "A"
            elif grade >= 90 and grade < 94:
                return "A-"
            elif grade >=87 and grade < 90:
                return "B+"
            elif grade >= 84 and grade < 87:
                return "B"
            elif grade >= 80 and grade < 84:
                return "B-"
            elif grade >= 77 and grade < 80:
                return "C+"
            elif grade >= 74 and grade < 77:
                return "C"
            elif grade >= 70 and grade < 74:
                return "C-"
            elif grade >= 67 and grade < 70:
                return "D+"
            elif grade >= 64 and grade < 67:
                return "D"
            elif grade >= 61 and grade < 64:
                return "D-"
            else:
                return "F"

        letter_grade = grade_converter(grade)

        print("Your letter grade is " + str(letter_grade) + "!")
    else:
        break

如果等级!= -1当用户没有输入&#34; -1&#34;该计划将继续。

每次进入循环时,它都会打印一个新的语句,这就是为什么print语句在&#34; if&#34;言。

否则:如果用户输入&#34; -1&#34;该程序将停止使用&#34; break&#34;句法。