英文:
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 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
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) > 25:
e.msgbox("Number is higher than 25, try again", "Error")
flag = True
if int(input) < 1:
e.msgbox("Number is smaller than 1, try again", "Error")
flag = True
except ValueError:
e.msgbox("Invalid input, try again", "Error")
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 = {
"Stoneling": {
"strength": "7",
"speed": "1",
"stealth": "25",`your text`
"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",
}
}
# The first screen the user is met with to edit the cards
while True:
choices = ["Add a new card to the catalogue",
"Search the catalogue",
"Delete a card from the catalogue",
"Output the entire catologue to terminal",
"Exit the program"]
input = e.buttonbox("Choose on of the options to edit the catalogue: ", "Monster Card Catalogue", choices)
if str(input) == "Exit the program":
exit()
if str(input) == "Add a new card to the catalogue":
while True:
name = (e.enterbox("Enter monster name", "New Monster Name"))
if name_characters(name):
break
else:
e.msgbox("Invalid characters, try again.")
continue
monster_cards[name] = {}
strength = 0
speed = 0
stealth = 0
cunning = 0
stats = ["strength", "speed", "stealth", "cunning"]
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("Enter a number for " + (stats[n]) + " up to 25", "New " + (stats[n]) + "stat"))
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论