我正在进行python刻字作业
90
或更高版本是A
,依此类推等等其他字母等级;但是当一个值作为负数输入时,我需要代码除了显示error
之外什么都不做。
这是我到目前为止所尝试的:
#Design a Python program to assign grade to 10 students
#For each student, the program first asks for the user to enter a positive number
#A if the score is greater than or equal to 90
#B if the score is greater than or equal to 80 but less than 90
#C if the score is greater than or equal to 70 but less than 80
#D if the score is greater than or equal to 60 but less than 70
#F is the score is less than 60
#Ihen the program dispalys the letter grade for this student.
#Use while loop to repeat the above grade process for 10 students.
keep_going = 'y'
while keep_going == "y":
num = float(input("Enter a number: "))
if num >= 90:
print("You have an A")
elif num >= 80:
print("You have an 3")
elif num >= 70:
print("You have an C")
elif num >= 60:
print("You have an D")
elif (num < 60 and <= 0:
print ("You have an F")
else:
print("lnvalid Test Score.")
答案 0 :(得分:2)
我看到三个问题,都在同一行:
elif (num < 60 and <= 0:
语法:num < 60 and <= 0
不是有效的表达式;应为num < 60 and num <= 0
逻辑:num <= 0
不是您想要的,应该是num >= 0
语法:您错过了结束括号)
。
如果你改变它们,它应该有效。
答案 1 :(得分:0)
grade = int(input("Enter Score:"))
print "FFFFFDCBAA"[grade//10] if grade >= 0 else "ERROR!!!!"
答案 2 :(得分:0)
你只需将你的elif改为60以下。
keep_going = 'y'
while keep_going == "y":
num = float(input("Enter a number: "))
if num >= 90:
print("You have an A")
elif num >= 80:
print("You have an 3")
elif num >= 70:
print("You have an C")
elif num >= 60:
print("You have an D")
elif 60 > num >= 0:
print ("You have an F")
else:
print("lnvalid Test Score.")