【问题标题】:Beginners Python. Loops and end function [duplicate]初学者 Python。循环和结束功能[重复]
【发布时间】:2018-02-04 00:32:06
【问题描述】:

我所学的是非常基础的。我从我正在学习的一本书中编写了一个简短的程序,我想稍微修改它,以便您可以再次循环并使用结束函数而不是简单的回答一次,程序结束。这就是我所拥有的:

WhatsMyGrady.py

grade=eval(input("Enter the number of your grade (0-100):"))
while grade !="end":
if grade>=90:
    print("You got an A!:)")
elif grade>=90:
    print("You got a B!")
elif grade>=80:
    print("You got a C.")
elif grade>=70:
    print("You got a D...")
elif grade>=60:
    print("you got an F.")
elif grade<59:
    print("You Fail!")
grade=input("select another grade or enter 'end' to quit")

Traceback(最近一次调用最后一次): 文件“C:\TheseusP&GS\Python\teaching_your_kids_code\what_my_grade.py”,第 4 行,在 如果等级>=90: TypeError:“str”和“int”的实例之间不支持“>=”

这是我收到的错误。

【问题讨论】:

  • 1:grade = int(input("Enter the number of your grade (0-100):")),2:两次条件grade &gt;= 90
  • 不要不要使用eval(input(...))

标签: python loops


【解决方案1】:

您正在用 >= 比较字符串和 int。相反,请执行此操作。

grade=input("Enter the number of your grade (0-100):")
while grade !="end":
    grade = int(grade)
    if grade>=90:
        print("You got an A!:)")
    elif grade>=90:
        print("You got a B!")
    elif grade>=80:
        print("You got a C.")
    elif grade>=70:
        print("You got a D...")
    elif grade>=60:
        print("you got an F.")
    elif grade<59:
        print("You Fail!")
    grade=input("select another grade or enter 'end' to quit")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 2019-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多