Skip to main content
 首页 » 编程设计

python之我写这段代码是为了在 5 个等级中取平均值并根据平均值显示一条消息,它正在运行

2025年05月04日12yjmyzz

我写这段代码是为了取 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 语句。

请您参考如下方法:

在 python 中不要使用 &,除非你处理的是位运算。 否则,您使用 和

您正在寻找:

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