[英]Assistance needed with basic Python program
當我遇到涉及循環的問題時,我正在嘗試學習編程,並且正在做一些練習。 這是問題:
創建一個程序以獲取用戶今年其課程的分數(向用戶詢問他們參加了多少課)。 告訴他們他們失敗了幾節課,他們的最佳成績和最差的成績。
我不知道如何顯示最佳標記和最差標記。 這是我得到的:
count = 0
total = 0
while True:
mark = input("Enter a mark (0-100) <-1 to exit> ")
if mark == -1:
break
elif mark < 50:
count += 1
total += mark
print "You failed",count,"class(es). "
再添加兩個輔助變量: worstmark
和bestmark
。
然后在循環中,說明輸入低於當前worstmark
還是高於bestmark
。 相應地分配值。
希望這會有所幫助:
# list of marks
marks = []
# get marks from user
while True:
mark = input("Enter a mark (0-100) <-1 to exit> ")
if mark < 0:
break
elif mark <= 100:
marks.append(mark)
# count number of classes failing
failing = len([f for f in marks if f<50])
best = max(marks)
worst = min(marks)
# check if a least one mark entered
if (len(marks) > 0):
print "The number of classes you are failing:",failing
print "Your best class score:",best
print "Your worst class score",worst
else:
print "Your are not taking any classes!"
演示:
$ python classes.py
Enter a mark (0-100) <-1 to exit> -1
Your are not taking any classes!
$ python classes.py
Enter a mark (0-100) <-1 to exit> 30
Enter a mark (0-100) <-1 to exit> 40
Enter a mark (0-100) <-1 to exit> 50
Enter a mark (0-100) <-1 to exit> 60
Enter a mark (0-100) <-1 to exit> 70
Enter a mark (0-100) <-1 to exit> 80
Enter a mark (0-100) <-1 to exit> -1
The number of classes you are failing: 2
Your best class score: 80
Your worst class score 30
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.