这是我一直在做的一个项目,这是一个统计数据选择系统的基线版本。
我尝试做的是两件事:一:防止用户输入大于40的值。第二:防止用户在没有收到值的情况下收到错误消息,但不会从一开始就循环整个段。
如果有人能为这些问题提出一个解决方案,或者提供一个新的概念来替代我写的东西,那就太好了。
如果答案是显而易见的或者写在其他地方,我道歉,我对Python和特别是Stackoverflow都是新手,而且我仍然掌握着正确的术语
谢谢
stealth = int(input("Stealth:"))
if stealth >= 40:
print ("Points Spent!")
else:
luck = int(input("Luck:"))
if stealth + luck >= 40:
print ("Points Spent!")
else:
perception = int(input("Perception:"))
if stealth + luck + perception >= 40:
print ("Points Spent!")
else:
swordsmanship = int(input("Swordsmanship:"))
if stealth + luck + perception + swordsmanship >= 40:
print ("Points Spent!")
else:
archery = int(input("Archery:"))
if stealth + luck + perception + swordsmanship + archery >= 40:
print ("Points Spent!")
else:
constitution = int(input("Constitution:"))
if stealth + luck + perception + swordsmanship + archery + constitution>= 40:
print ("Points Spent!")
else:
agility = int(input("Agility:"))发布于 2020-05-11 21:47:02
如果我对问题理解正确,您将向用户显示值列表,直到到目前为止输入的值的总和为>=40。一旦达到这一点,您将停止打印"Points reached“
我认为你可以这样做:
def get_input(msg):
ret = None
while True:
val = input(msg)
if len(val) > 0:
ret = int(val)
break
return ret
def imput_and_print(s,lst,index):
if index < len(lst):
#val = int(input(lst[index] + " : "))
val = get_input(input(lst[index] + " : "))
s += val
if(s>= 40):
print ("Points Spent!")
else:
imput_and_print(s,lst,index+1)
tries_list = ["Stealth","Luck","Perception","Swordsmanship","Archery","Constitution","Agility"]
imput_and_print(0,tries_list,0)https://stackoverflow.com/questions/61730858
复制相似问题