首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python Elif Flowdown Value Selector -如何设置值上限并防止错误?

Python Elif Flowdown Value Selector -如何设置值上限并防止错误?
EN

Stack Overflow用户
提问于 2020-05-11 21:27:30
回答 1查看 29关注 0票数 0

这是我一直在做的一个项目,这是一个统计数据选择系统的基线版本。

我尝试做的是两件事:一:防止用户输入大于40的值。第二:防止用户在没有收到值的情况下收到错误消息,但不会从一开始就循环整个段。

如果有人能为这些问题提出一个解决方案,或者提供一个新的概念来替代我写的东西,那就太好了。

如果答案是显而易见的或者写在其他地方,我道歉,我对Python和特别是Stackoverflow都是新手,而且我仍然掌握着正确的术语

谢谢

代码语言:javascript
复制
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:"))
EN

回答 1

Stack Overflow用户

发布于 2020-05-11 21:47:02

如果我对问题理解正确,您将向用户显示值列表,直到到目前为止输入的值的总和为>=40。一旦达到这一点,您将停止打印"Points reached“

我认为你可以这样做:

代码语言:javascript
复制
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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61730858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档