我写了这个代码,取5个等级取平均值并根据平均值显示消息,这里是代码:
counter=0
grd=0
while counter<5:
a=int(input('Please enter a grade.'))
grd=grd+a
counter=counter+1
avg=int(grd/5)
if avg>=90&avg<=100:
print('Your average is', avg, 'Excellent!')
if avg>=80&avg<=89:
print('Your average is', avg, 'Very Good!')
if avg>=70&avg<=79:
print('Your average is', avg, 'Good')
if avg>=60&avg<=69:
print('Your average is', avg, 'Satisfactory')
if avg<=50:`enter code here`
print('Your average is', avg, 'Go Home
我遇到的问题是,无论平均值是多少,它们都会显示前4个if语句。
答案 0 :(得分:8)
你不能使用&amp;在python中,除非你正在处理按位操作。
否则,您使用and
您正在寻找:
counter=0
grd=0
while counter<5:
a=int(input('Please enter a grade.'))
grd=grd+a
counter=counter+1
avg=int(grd/5)
if avg>=90 and avg<=100:
print('Your avarage is', avg, 'Excellent!')
if avg>=80 and avg<=89:
print('Your avarage is', avg, 'Very Good!')
if avg>=70 and avg<=79:
print('Your avarage is', avg, 'Good')
if avg>=60 and avg<=69:
print('Your avarage is', avg, 'Satisfactory')
if avg<=50:
print('Your avarage is', avg, 'Go Home
答案 1 :(得分:0)
这是一个范围测试,所以你也可以像这样编码:
import bisect as bisect
counter,grd = 0,0
while counter < 5:
a = float(input('Please enter a grade.'))
grd += a
counter += 1
avg = grd/5
breakPoint = [50,60,70,80,90]
comment = [' Go Home','',' Satisfactory',' Good',' Very Good!',' Excellent!']
index = bisect.bisect(breakPoint,avg)
remark = 'Your avarage is {}' + comment[index]
print (remark.format(avg) )
答案 2 :(得分:0)
如之前的回复中所述,请勿使用&amp;而不是和。也处理非数字值,不要让他们破坏你的程序将是有益的。
counter=0
grd=0
while counter<5:
a=input('Please enter a grade.')
try:
a = int(a) # check if input is convertible to integer
grd=grd+a
counter=counter+1
except: #if not, print a message
print ("enter numeric value ")
a = 0 # assign zero
counter = counter # reset counter increment
avg=int(grd/5)
if avg>=90 and avg<=100:
print('Your avarage is', avg, 'Excellent!')
elif avg>=80 and avg<=89:
print('Your avarage is', avg, 'Very Good!')
elif avg>=70 and avg<=79:
print('Your avarage is', avg, 'Good')
elif avg>=60 and avg<=69:
print('Your avarage is', avg, 'Satisfactory')
elif avg<=50:
print('Your avarage is', avg, 'Go Home')
else:
print ("some other message for further development")
答案 3 :(得分:0)
m1=int(input("Enter 1st Sub marks"))
m2=int(input("Enter 2nd Sub marks"))
m3=int(input("Enter 3rd Sub marks"))
m4=int(input("Enter 4th Sub marks"))
m5=int(input("Enter 5th Sub marks"))
avg=(m1+m2+m3+m4+m5)/5
print (int(avg))
if (avg>=90 and avg<=100):
print ("Grade A")
elif (avg>=80 and avg<90):
print ("Grade B")
elif (avg>=70 and avg<80):
print ("Grade C")
elif (avg>=60 and avg<70):
print ("Grade D")
elif (avg>=50 and avg<60):
print ("Grade E")
elif (avg>100):
print ("Entered Marks are out of range")
else:
print ("FAIL")