擅长:python、mysql、java
<p>这里有一些值得你学习的东西,我认为它能满足你的所有需求:</p>
<pre><code>people = 0
a = 0
while True:
while True:
try:
n = int(input("PERSONS WEIGHT?"))
break
except ValueError as ex:
print("You didn't type a number. Try again")
people += 1
a += int(n)
while True:
answer = input("Do we continue adding people ? y/n")
if answer in ["y", "n"]:
break
print("You typed something wrong , add another value ")
if answer == 'n':
break
limit = 300
if a > limit:
print("Total weight is %d which exceeds %d so the lift is overloaded" % (a, limit))
else:
print("Total weight is %d which does not exceed %d so the lift can be operated" % (a, limit))
</code></pre>
<p>添加的主要思想是,每个输入必须有单独的循环,然后是一个外部循环,以便能够输入多个权重</p>
<p>将<code>people = 0</code>移出循环也很重要,这样它就不会一直重置回<code>0</code>,并以同样的方式初始化<code>a</code></p>