Hello Stack社区
我在询问用户输入时使用了if或语句,如果用户输入的值超出了我的if或语句,那么我会显示一条消息,提示“请输入1到500之间的有效数字”向用户的另一个输入请求。
所以我已经完成了目标的2/3。我已经拒绝了原始输入,但是已经重新提出了要求,但是在重新询问用户之后,我无法找到仅保留新值的方法。相反,我的程序保留了所需参数之外的原始用户输入,然后在重新设置后保留了新输入并将它们组合为一个值。这是在Python IDLE 3.6上。
确切的问题:如何从用户输入中删除原始输入值,并仅在重新设置后保留新值?
这是我的代码:
O(N^2)
答案 0 :(得分:2)
您可以使用无限循环不断询问用户有效输入,直到用户输入一个有效输入为止。请注意,您还应该使用<div id="home" class="section">
<div class="cover">
<svg viewBox="0 0 1366 768" width="100%" height="100%" preserveAspectRatio="xMinYMin meet">
<defs>
<path id="shape" d="M1039.5,770.5a125.77,125.77,0,0,0-17-32c-12.32-16.72-25.68-25.84-33-31-6-4.23-59.88-42.55-100-90-13.64-16.14-20.57-24.48-26-38-15-37.48-3.73-73.65,0-85,8.68-26.45,23-43.26,35-57,19-21.82,33.56-29.9,67-54,33.68-24.27,55.39-39.91,77-60,40.56-37.69,35.94-49.35,81-96,34.18-35.39,51.27-53.08,79-65,7.79-3.35,76-31.44,140,2a142,142,0,0,1,31,22l7.5,7.5V770.5Z" />
<clipPath id="art">
<use xlink:href="#shape" />
</clipPath>
</defs>
<image xlink:href="http://1.bp.blogspot.com/-xV_Q0sj_3HQ/T4NjscM0ibI/AAAAAAAAB_U/DKjSJvD0vls/s1600/Simple+unique+odd+weird+wallpapers+minimalistic+%2310.jpg" width="1366" height="768" preserveAspectRatio="xMidYMid slice" />
<use xlink:href="#shape" id="background" />
<image xlink:href="http://1.bp.blogspot.com/-xV_Q0sj_3HQ/T4NjscM0ibI/AAAAAAAAB_U/DKjSJvD0vls/s1600/Simple+unique+odd+weird+wallpapers+minimalistic+%2310.jpg" width="1366" height="768" preserveAspectRatio="xMidYMid slice" id="middleground" />
<use xlink:href="#shape" id="foreground" />
</svg>
</div>
</div>
块来确保用户输入有效的整数:
try-except
由于您要针对座位的不同部分提出相同的问题,因此您可以将不同部分的价格存储在字典中,然后在计算总收入时遍历各个部分以提出问题:>
while True:
try:
resultsA = int(input("lease enter how many seats you sold is section A:"))
if 1 <= resultsA <= 300:
break
raise RuntimeError()
except (ValueError, RuntimeError):
print("Please enter a valid number from 1 to 300.")
答案 1 :(得分:1)
您可以轻松地创建一个函数,通过其中的try / except块来完成您的任务:
def int_input(prompt, lower, higher, bad_input='bad input:'):
while True:
try:
r = int(input(prompt))
if r < lower or r > higher:
raise ValueError('enter a number between {} and {}'.format(lower, higher))
except ValueError as e:
print(bad_input, e)
else:
return r
然后在您的代码中使用它:
resultsA = int_input(
'enter no. of seats sold in section A',
1,
200,
'invalid input'
)
答案 2 :(得分:1)
尝试这个:
def main():
resultsA = int(input("Please enter how many seats you sold is section A:"))
while(resultsA < 1 or resultsA > 300):
resultsA = int(input("Please enter a valid number from 1 to 300:"))
resultsB = int(input("Please enter how many seats you sold is section B:"))
while(resultsB < 1 or resultsB > 500):
resultsB = int(input("Please enter a valid number from 1 to 500:"))
resultsC = int(input("Please enter how many seats you sold is section C:"))
while(resultsC < 1 or resultsC > 200):
resultsC = int(input("Please enter a valid number from 1 to 200:"))
finalResultA = 20*resultsA
finalResultsB = 15*resultsB
finalResultsC = 10*resultsC
trulyFinal = finalResultA + finalResultsB + finalResultsC
print ("Congratulations, here is your total revenue from tickets: $",trulyFinal)