简体   繁体   English

尝试并除以列表和多个输入

[英]Try and Except with a list and multiple inputs

I am making a D&D style character generator and I am rolling the stats for them and allowing them to allocate them to the ability score they want. 我正在制作D&D风格的角色生成器,并为他们滚动统计数据,并允许他们将其分配给所需的能力得分。 I would like to have the ability to start at the same stat they were at vs the entire section over again. 我想有能力从与整个部分相同的状态开始。

Here is what I have 这是我所拥有的

from random import randint
def char_stats():
    # roll 4 D6s drop the lowest number and add the highest 3
    s1,s2,s3,s4,s5,s6 = ([],[],[],[],[],[])
    for x in range(4):
        s1.append(randint(1,6))
        s2.append(randint(1,6))
        s3.append(randint(1,6))
        s4.append(randint(1,6))
        s5.append(randint(1,6))
        s6.append(randint(1,6))
    stat1 = sorted(s1)
    stat2 = sorted(s2)
    stat3 = sorted(s3)
    stat4 = sorted(s4)
    stat5 = sorted(s5)
    stat6 = sorted(s6)
    return sum(stat1[1:]),sum(stat2[1:]),sum(stat3[1:]),sum(stat4[1:]),sum(stat5[1:]),sum(stat6[1:])

a = list(char_stats())
print "Please choose one of the following for your stat: {}".format(a)
while len(a) > 0:
    try:
        Strength = int(raw_input('Please input one of these stats for your Strength:\n'))
        if Strength in a:
            a.remove(Strength)
            print a
        Wisdom = int(raw_input('Please input one of these stats for your Wisdom:\n'))
        if Wisdom in a:
            a.remove(Wisdom)
            print a
        Intelligence = int(raw_input('Please input one of these stats for your Intelligence:\n'))
        if Intelligence in a:
            a.remove(Intelligence)
            print a
        Constitution = int(raw_input('Please input one of these stats for your Constitution:\n'))
        if Strength in a:
            a.remove(Constitution)
            print a
        Dexterity = int(raw_input('Please input one of these stats for your Dexterity:\n'))
        if Dexterity in a:
            a.remove(Dexterity)
            print a
        Charisma = int(raw_input('Please input one of these stats for your Charisma:\n'))
        if Charisma in a:
            a.remove(Charisma)
    except ValueError:
        print "Incorrect Input"
        continue

I have tried nesting each of the if statements (which I believe is very bad form) and have similar results. 我已经尝试嵌套每个if语句(我认为这是非常糟糕的形式),并且结果相似。 I have also tried grouping all the inputs into the try and not the calculations and have gotten the same results. 我还尝试将所有输入分组到try中而不是计算中,并且得到了相同的结果。 Any advice? 有什么建议吗?

You need to use the "loop until valid" logic for both the format (int) of the input, and the value (is it in the list of rolled stats?). 您需要对输入的格式(int)和值(是否在滚动统计信息列表中)使用“直到有效的循环”逻辑。 The basic logic is this: 基本逻辑是这样的:

while True:
    # get input
    # check input
    # if input is valid,
    #   break

In your case, this looks something like 就您而言,这看起来像

while True:
    user = input("Please enter a stat to use")
    if user.isnumeric():
        stat_choice = int(user)
        if stat_choice in a:
            break

Now, to make effective use of this, you need to parametrize your six stats and put those into a loop: 现在,为了有效利用这一点,您需要对六个统计参数进行参数化并将其放入循环中:

stat_name = ["Strength", "Wisdom", ...]
player_stat = [0, 0, 0, 0, 0, 0]

for stat_num in range(len(player_stat)):
    while True:
        user = input("Please input one of these stats for your" + \
                      stat_name[stat_num] + ": ")
        # Validate input as above

    player_stat[stat_num] = stat_choice

Note that you can similarly shorten your char_stats routine to a few lines. 请注意,您可以类似地将char_stats例程缩短到几行。

Does that get you moving? 那会让你动起来吗?

You have at least one function in your code ( char_stats ) so I feel like you know how to do functions. 您的代码中至少有一个函数( char_stats ),所以我觉得您知道如何做函数。

This is a great place to use a function. 这是使用函数的好地方。

My suggestion, for this code, would be to write a function that incorporates the try/except, the asking of the question, and the checking against the stats list. 对于此代码,我的建议是编写一个包含try / except,问题询问以及对统计信息列表进行检查的函数。

Something like this: 像这样:

def pick_a_stat(stats, prompt):
    """Prompt the user to pick one of the values in the `stats` list,
    and return the chosen value. Keep prompting until a valid entry
    is made.
    """

    pass  # Your code goes here.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM