如何循环此代码?

2024-09-29 17:21:14 发布

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

我如何用“你想再次运行这个”的问题来循环这个代码?你知道吗

count = 1
total = 0

markslist = list()

while count <= 10:
    newmark = input("Input Mark Out Of 100 (" + str(count) + ") ")
    markslist.append(newmark)
    if newmark > 100: 
        import sys      
        sys.exit("Error Mark is greater than 100 re-run program")
    total = total + newmark 
    count = count + 1

average = (total / 10)

print "Average Mark = " + str(average)

Tags: of代码inputcountsysoutlisttotal
1条回答
网友
1楼 · 发布于 2024-09-29 17:21:14

只需将整个代码包装在一个类似于以下内容的循环中:

while True:
    again = input('Go? (y/n): ')
    if again != 'y':
        break

    # The entirety of the rest of your program would go here.

相关问题 更多 >

    热门问题