我是python的新手,正在努力打造一本成绩簿。这些是说明。
例:
Enter the student's name:Bobby Baylor
How many grades does Bobby Baylor have?4
Enter grade 0:100
Enter grade 1:80
Enter grade 2:90
Enter grade 3:90
Bobby Baylor made A
Enter the student's name:Betty Baylor
How many grades does Betty Baylor have?3
Enter grade 0:100
Enter grade 1:80
Enter grade 2:80
Betty Baylor made B
Enter the student's name:-1
这是我尝试过的代码,但是我不断收到一个错误,那就是我的第一条if语句无效。我相信有更多的错误,但我无法超越这第一个错误。
def main():
while true:
student_name=(input("Enter the student's name:")
if student_name==-1:
break
else:
num_exams=int(input("How many grades does"+student_name+"have?"))
counter=0
test_num=int(input('Enter grade'+counter+':'))
while counter<num_exams-1:
if counter < num_exams:
counter=counter+1
test_num=int(input('Enter grade'+counter+':'))
for n in range(test_num):
total_exams+=test_num
avg=total_exams/num_exams
if avg>=90:
print(student_name, "made A")
elif 80<=avg<90:
print(student_name, "made B")
elif 70<=avg<80:
print(student_name, "made C")
elif 60<=avg<70:
print(student_name, "made D")
main()
发布于 2018-06-13 15:22:23
while True:
student_name=(input("Enter the student's name:"))
if student_name=='-1':
break
else:
num_exams=int(input("How many grades does"+student_name+"have?"))
counter=0
test_num=[]
total_exams=0
while counter < num_exams:
counter =counter+1
test_num.append(int(input('Enter grade'+ str(counter)+':')))
for n in range(0,num_exams):
total_exams+=test_num[n]
avg=total_exams/num_exams
if avg>=90:
print(student_name, "made A")
elif 80<=avg<90:
print(student_name, "made B")
elif 70<=avg<80:
print(student_name, "made C")
elif 60<=avg<70:
print(student_name, "made D")
else:
print("Fail")
发布于 2018-06-13 16:44:16
def main():
while True: # correct spelling
student_name= input("Enter the student's name:") # fixed ( not needed
if student_name == "-1": # this is a string, not a number
break # you could as well return None here
# else: # as pointed out by Rishav this is not needed
num_exams=int(input("How many grades does "+student_name+ " have?"))
# list comp that uses range() to ask c times
# fix for the string concattenation (cant do "x" + 3 + "x")
grades = [int(input('Enter grade '+str(c+1)+':')) for c in range(num_exams)]
total = sum(grades) # use build-in to summ the grades in list
average = total / num_exams # calc average
if average >=90: # no need to double check, first one will be usd
print(student_name, "made A")
elif average>=80: # so comparing for <90 is not needed at all
print(student_name, "made B")
elif average >=70 :
print(student_name, "made C")
else:
print(student_name, "made D")
main()
你可以用:
# grades = [int(input('Enter grade '+str(c+1)+':')) for c in range(num_exams)]
grades = []
for k in range(num_exams): # range does 0... num_exams-1
grades.append(int(input("Enter grade {}:".format(k+1))))
https://stackoverflow.com/questions/-100005373
复制相似问题