我是python的新手,循环时总是遇到此问题。我应该怎么做才能使“打印”和“输入”合而为一?
请帮助我^。^ 这是我的代码
subjects = int(input("How many subjects do you have?"))
subject_grades = []
sum = 0
for e in range(0, subjects):
print("Enter the grade of your subject number ", e+1, ": ", sep="")
grade = int(input())
subject_grades.append(grade)
sum+=grade
average= sum/len(subject_grades)
print("The sum of your grades is", sum, "and the average is", average)
我希望这段代码的输出为:
How many subjects do you have?3
Enter the grade of your subject number 1: 1
Enter the grade of your subject number 2: 2
Enter the grade of your subject number 3: 3
The sum of your grades is 6 and the average is 2.0
但实际输出是:
How many subjects do you have?3
Enter the grade of your subject number 1:
1
Enter the grade of your subject number 2:
2
Enter the grade of your subject number 3:
3
The sum of your grades is 6 and the average is 2.0
答案 0 :(得分:0)
使用
grade = int(input("Enter the grade of your subject number " + str(e+1) + ": "))
代替
print("Enter the grade of your subject number ", e+1, ": ", sep="")
grade = int(input())