How do I make a for loop that has range in it loop back to the top of the loop without changing the range variable? I have code

huangapple go评论61阅读模式
英文:

How do I make a for loop that has range in it loop back to the top of the loop without changing the range variable? I have code

问题

以下是您提供的代码的翻译部分:

# 检查只有这些字符出现在名称中
def name_characters(input_string):
    allowed_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
    
    for char in input_string:
        if char not in allowed_chars:
            return False
    
    return True

# 确保数字不大于25或小于1
def input_stat_error(input):
    flag = False
    while True:
        try:
            if int(input) > 25:
                e.msgbox("数字大于25,请重试", "错误")
                flag = True

            if int(input) < 1:
                e.msgbox("数字小于1,请重试", "错误")
                flag = True
        
        except ValueError:
            e.msgbox("无效输入,请重试", "错误")
            flag = True

        return None

# 使界面看起来更好,节省空间
import easygui as e

# 用户开始的卡片目录
monster_cards = {
    "Stoneling": {
        "strength": "7",
        "speed": "1",
        "stealth": "25",
        "cunning": "15",
    },
    "Vexscream": {
        "strength": "1",
        "speed": "6",
        "stealth": "21",
        "cunning": "19",
    },
    "Dawnmirage": {
        "strength": "5",
        "speed": "15",
        "stealth": "18",
        "cunning": "22",
    },
    "Blazegolem": {
        "strength": "15",
        "speed": "20",
        "stealth": "23",
        "cunning": "6",
    },
    "Websnake": {
        "strength": "7",
        "speed": "15",
        "stealth": "10",
        "cunning": "5",
    },
    "Moldvine": {
        "strength": "21",
        "speed": "18",
        "stealth": "14",
        "cunning": "5",
    },
    "Vortexwing": {
        "strength": "19",
        "speed": "13",
        "stealth": "19",
        "cunning": "2",
    },
    "Rotthing": {
        "strength": "16",
        "speed": "7",
        "stealth": "14",
        "cunning": "12",
    },
    "Froststep": {
        "strength": "14",
        "speed": "14",
        "stealth": "17",
        "cunning": "4",
    },
    "Wispghoul": {
        "strength": "17",
        "speed": "19",
        "stealth": "3",
        "cunning": "2",
    }
}

# 用户遇到的第一个屏幕以编辑卡片
while True:
    choices = ["将新卡片添加到目录",
               "搜索目录",
               "从目录中删除卡片",
               "将整个目录输出到终端",
               "退出程序"]

    input = e.buttonbox("选择一个选项以编辑目录:", "怪物卡目录", choices)
    if str(input) == "退出程序":
        exit()

    if str(input) == "将新卡片添加到目录":
        while True:
            name = (e.enterbox("输入怪物名称", "新怪物名称"))
            if name_characters(name):
                break
            else:
                e.msgbox("包含无效字符,请重试。")
                continue

        monster_cards[name] = {}
        strength = 0
        speed = 0
        stealth = 0
        cunning = 0
        stats = ["strength", "speed", "stealth", "cunning"]
        stats_names = [strength, speed, stealth, cunning]

        # 为每个属性循环************************************************** 需要修复。发生错误时不会回到原始属性。
        x = range(4)
        for n in x:
            while True:
                (stats_names[n]) = int(e.enterbox("输入" + (stats[n]) + "的数字,范围在1到25之间", "新" + (stats[n]) + "属性"))
                monster_cards[name][stats[n]] = (stats_names[n])
                input_stat = (stats_names[n])
                print(input_stat)
                flag = input_stat_error(input_stat)
                if flag is True:
                    continue
                else:
                    break

请注意,这个翻译只包含您提供的代码部分,不包括代码之外的任何内容。

英文:

I have a function that makes sure that a stat number is between 1-25 in a range for loop. If the number is bigger than 25 or smaller than 1 or has an invalid input it makes the flag = True, and then it continues. For some reason though it doesn't go back to the stat that had an error, it just moves onto the next one.

I've already tried to use break continue and all that but none if it works.

Here is the code:

# checks that only these characters are in a name
def name_characters(input_string):
allowed_chars = &quot;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&quot;
for char in input_string:
if char not in allowed_chars:
return False
return True
`makes sure the number is not higher or lower than 1 or 25`
# makes sure the number is not higher or lower than 1 or 25.
def input_stat_error(input):
flag = False
while True:
try:
if int(input) &gt; 25:
e.msgbox(&quot;Number is higher than 25, try again&quot;, &quot;Error&quot;)
flag = True
if int(input) &lt; 1:
e.msgbox(&quot;Number is smaller than 1, try again&quot;, &quot;Error&quot;)
flag = True
except ValueError:
e.msgbox(&quot;Invalid input, try again&quot;, &quot;Error&quot;)
flag = True
return None   
# Makes it look a lot better and saves space
import easygui as e
# This is the card catalogue the user starts with
monster_cards = {
&quot;Stoneling&quot;: {
&quot;strength&quot;: &quot;7&quot;,
&quot;speed&quot;: &quot;1&quot;,
&quot;stealth&quot;: &quot;25&quot;,`your text`
&quot;cunning&quot;: &quot;15&quot;,
},
&quot;Vexscream&quot;: {
&quot;strength&quot;: &quot;1&quot;,
&quot;speed&quot;: &quot;6&quot;,
&quot;stealth&quot;: &quot;21&quot;,
&quot;cunning&quot;: &quot;19&quot;,
},
&quot;Dawnmirage&quot;: {
&quot;strength&quot;: &quot;5&quot;,
&quot;speed&quot;: &quot;15&quot;,
&quot;stealth&quot;: &quot;18&quot;,
&quot;cunning&quot;: &quot;22&quot;,
},
&quot;Blazegolem&quot;: {
&quot;strength&quot;: &quot;15&quot;,
&quot;speed&quot;: &quot;20&quot;,
&quot;stealth&quot;: &quot;23&quot;,
&quot;cunning&quot;: &quot;6&quot;,
},
&quot;Websnake&quot;: {
&quot;strength&quot;: &quot;7&quot;,
&quot;speed&quot;: &quot;15&quot;,
&quot;stealth&quot;: &quot;10&quot;,
&quot;cunning&quot;: &quot;5&quot;,
},
&quot;Moldvine&quot;: {
&quot;strength&quot;: &quot;21&quot;,
&quot;speed&quot;: &quot;18&quot;,
&quot;stealth&quot;: &quot;14&quot;,
&quot;cunning&quot;: &quot;5&quot;,
},
&quot;Vortexwing&quot;: {
&quot;strength&quot;: &quot;19&quot;,
&quot;speed&quot;: &quot;13&quot;,
&quot;stealth&quot;: &quot;19&quot;,
&quot;cunning&quot;: &quot;2&quot;,
},
&quot;Rotthing&quot;: {
&quot;strength&quot;: &quot;16&quot;,
&quot;speed&quot;: &quot;7&quot;,
&quot;stealth&quot;: &quot;14&quot;,
&quot;cunning&quot;: &quot;12&quot;,
},
&quot;Froststep&quot;: {
&quot;strength&quot;: &quot;14&quot;,
&quot;speed&quot;: &quot;14&quot;,
&quot;stealth&quot;: &quot;17&quot;,
&quot;cunning&quot;: &quot;4&quot;,
},
&quot;Wispghoul&quot;: {
&quot;strength&quot;: &quot;17&quot;,
&quot;speed&quot;: &quot;19&quot;,
&quot;stealth&quot;: &quot;3&quot;,
&quot;cunning&quot;: &quot;2&quot;,
}
}
# The first screen the user is met with to edit the cards
while True:
choices = [&quot;Add a new card to the catalogue&quot;,
&quot;Search the catalogue&quot;,
&quot;Delete a card from the catalogue&quot;,
&quot;Output the entire catologue to terminal&quot;,
&quot;Exit the program&quot;]
input = e.buttonbox(&quot;Choose on of the options to edit the catalogue: &quot;, &quot;Monster Card Catalogue&quot;, choices)
if str(input) == &quot;Exit the program&quot;:
exit()
if str(input) == &quot;Add a new card to the catalogue&quot;:
while True:
name = (e.enterbox(&quot;Enter monster name&quot;, &quot;New Monster Name&quot;))
if name_characters(name):
break
else:
e.msgbox(&quot;Invalid characters, try again.&quot;)
continue
monster_cards[name] = {}
strength = 0
speed = 0
stealth = 0
cunning = 0
stats = [&quot;strength&quot;, &quot;speed&quot;, &quot;stealth&quot;, &quot;cunning&quot;]
stats_names = [strength, speed, stealth, cunning]
# Loops for each stat ****************************************************** NEEEEEED TO FIX. WHEN ERROR IT DOESNT LOOP BACK TO ORIGINAL STAT.
x = range(4)
for n in x:
while True:
(stats_names[n]) = int(e.enterbox(&quot;Enter a number for &quot; + (stats[n]) + &quot; up to 25&quot;, &quot;New &quot; + (stats[n]) + &quot;stat&quot;))
monster_cards[name][stats[n]] = (stats_names[n])
input_stat = (stats_names[n])

please


print(input_stat)
flag = input_stat_error(input_stat)
if flag is True:
continue
else:
break

答案1

得分: 2

我尝试了解这个问题一些,所以如果我对你的意图有任何误解,请告诉我。

首先,据我所知,你不能在Python中将变量命名为input,因为这是在运行时接受用户输入的函数的名称。

至于你的问题,continue 的设计目的就是跳过当前迭代。听起来你想阻止循环继续调整那个值。这在使用for循环时可能会令人困惑,所以你可能想要使用一个带有控制变量的While循环,只有在满足条件时显式递增该变量。

这允许你在保持控制变量不变的情况下实际上“重做”循环。

英文:

I've tried to understand this a bit, so if I have any misconceptions about your intentions, please let me know.

I want to mention, firstly, as far as I'm aware, you can't name a variable input in python, as that's the name of a function to accept user input during runtime.

To get to your question, continue is designed for just that; skipping an iteration. It sounds like you want to prevent the loop from continuing to adjust that value. This can be confusing with a for loop, so you may want to hop to a While loop with a control variable you explicitly increment only when it meets your condition.

This allows you to essentially "redo" the loop while keeping your control variable the same.

huangapple
  • 本文由 发表于 2023年5月29日 12:02:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定