While循环作为成绩计算器,与完成的作业数量#相关

2024-09-28 01:30:05 发布

您现在位置:Python中文网/ 问答频道 /正文

到目前为止,这是我的while循环。我问用户他们完成了多少作业,只能在1到10之间(从0到5分)

但是,如果他们回答10以下的任何问题,我希望to代码自动将其输入为零,而不是用户必须为他们尚未完成的每个任务手动输入0

现在我的代码会要求用户输入10次成绩,但我只想根据用户完成多少作业的答案输入成绩。因此,如果他们回答了7个问题,那么这个问题将被呈现7次,三个0分将自动计入平均值

while True:
   
    result = input("How many assignments did you complete? ")
   
  if result.isdigit() and 0 <= int(result) <= 10:
         break;
     print ("Try a number between 1 and 10") 
total = 0
gradeCount = 0
while gradeCount < 10:
    grade = float(input('What was assignment score: '))
    if grade < 0 or grade > 5:
        print('It should be a number from 0 to 5')
    else:
        gradeCount += 1
        total += grade
aaverage = total / 30 * 100
print('Average: ', aaverage)

Tags: andto代码用户numberinputif作业
2条回答
while True:
   
    result = input("How many assignments did you complete? ")
    if result.isdigit() and 0 <= int(result) <= 10:
         break;
    print ("Try a number between 1 and 10") 
total = 0
gradeCount = 0
while gradeCount < int(result):
    grade = float(input('What was assignment score: '))
    if grade < 0 or grade > 5:
        print('It should be a number from 0 to 5')
    else:
        gradeCount += 1
        total += grade
aaverage = total / 50 * 100
print('Average: ', aaverage)

数一数作业的数量。此外,你还必须除以50,因为最多分配10个作业,所以得50分

while 1:
    result = input("How many assignments did you complete? ")
    if result.isdigit() and 0 <= int(result) <= 10:
         result = int(result)
         break
     print ("Try a number between 1 and 10") 
total = 0
for i in range(result):
    while 1:
        grade = float(input('What was assignment score: '))
        if 0 <= grade <= 5:
            break
        else:
            print('It should be a number from 0 to 5')
    total += grade
aaverage = total / 10
print('Average: ', aaverage)

相关问题 更多 >

    热门问题