如果满足if语句的条件,如何停止while语句中的错误消息



你好,我的代码有问题,我不确定修复方法是什么,代码应该打印阴影识别,并在输入正确时打印所需信息,并且当输入不正确时打印错误消息并且再次询问阴影的名称,然而当输入正确时。

import time
print("Hello, welcome to the Persona 5 Helper")
time.sleep(2)
print("Type in the name of the shadow you're facing and the app will show you its weaknessesn")
time.sleep(2)
shadows = {"Crypt-dwelling Demon": "Jack-O lanternn" "Version P5: Inherit = Fire, Reflects = NA, Absorbs = Fire, Block = NA, Resists = NA, Weak = Gun/Ice/Windn"
"Version P5R: Inherit = NA, Reflects = NA, Absorbs = Fire, Block = NA, Resists = NA, Weak = Ice/Wind",
"Beguiling Girl": "Pixien" "Version Both: Inherit = Elec, Reflects = NA, Absorbs = NA, Block = NA, Resists = Elec/Bless, Weak = Gun/Ice/Curse",
"Bedside Brute": "Incubusn" "Version Both: Inherit = Ailment, Reflects = NA, Absorbs = NA, Block = NA, Resists = Elec, Weak = Gun/Bless",
"Gallows Flower": "Mandraken" "Version Both: Inherit = Elec, Reflects = NA, Absorbs = NA, Block = NA, Resists = Elec/Bless, Weak = Wind",
"Dirty Two-horned Beast": "Bicornn" "Version Both: Inherit = Wind, Reflects = NA, Absorbs = NA, Block = NA, Resists = Curse, Weak = Elec",
"Apprentice in a jug": "Agathionn" "Version Both: Inherit = Elec, Reflects = NA, Absorbs = NA, Block = NA, Resists = Elec/Gun, Weak = Wind"}
shadowName = ""
while shadowName not in shadows:
shadowName = input("Name of shadow: ")
print('ERROR: Make sure your spelling is correct or the shadow is an existing one...')
if shadowName in shadows:
print("Shadow Identified...")
print(shadows[shadowName])

执行代码时,shadowName初始化为";。

由于"不在阴影中(while条件(,则您的程序将请求";阴影的名称";(input(,无论您的输入如何,它都将始终打印第一条错误消息。

不能在for循环中更改这两行,因为对于第一个名称,错误消息仍然会出现。

一种解决方案是:不使用"初始化shadowName&";,请求输入。

此外,更改行顺序,使错误消息出现在输入询问之前。你不需要if条件,因为如果不是(shadowName在shadows中((即if条件为False(,那么程序无论如何都会被困在while循环中:

shadowName = input("Name of shadow: ")
while shadowName not in shadows:
print('ERROR: Make sure your spelling is correct or the shadow is an existing one...')
shadowName = input("Name of shadow: ")
print("Shadow Identified...")
print(shadows[shadowName])

打印函数在输入后立即执行,因此显示错误。试试这个。

import time
print("Hello, welcome to the Persona 5 Helper")
time.sleep(2)
print("Type in the name of the shadow you're facing and the app will show you its weaknessesn")
time.sleep(2)
shadows = {"Crypt-dwelling Demon": "Jack-O lanternn" "Version P5: Inherit = Fire, Reflects = NA, Absorbs = Fire, Block = NA, Resists = NA, Weak = Gun/Ice/Windn"
"Version P5R: Inherit = NA, Reflects = NA, Absorbs = Fire, Block = NA, Resists = NA, Weak = Ice/Wind",
"Beguiling Girl": "Pixien" "Version Both: Inherit = Elec, Reflects = NA, Absorbs = NA, Block = NA, Resists = Elec/Bless, Weak = Gun/Ice/Curse",
"Bedside Brute": "Incubusn" "Version Both: Inherit = Ailment, Reflects = NA, Absorbs = NA, Block = NA, Resists = Elec, Weak = Gun/Bless",
"Gallows Flower": "Mandraken" "Version Both: Inherit = Elec, Reflects = NA, Absorbs = NA, Block = NA, Resists = Elec/Bless, Weak = Wind",
"Dirty Two-horned Beast": "Bicornn" "Version Both: Inherit = Wind, Reflects = NA, Absorbs = NA, Block = NA, Resists = Curse, Weak = Elec",
"Apprentice in a jug": "Agathionn" "Version Both: Inherit = Elec, Reflects = NA, Absorbs = NA, Block = NA, Resists = Elec/Gun, Weak = Wind"}
shadowName = ""
while shadowName not in shadows:
shadowName = input("Name of shadow: ")

if shadowName in shadows:
print("Shadow Identified...")
print(shadows[shadowName])
break
else:
print('ERROR: Make sure your spelling is correct or the shadow is an existing one...')

我会稍微重新安排你的while循环:

while True:
shadowName = input("Name of shadow: ")

if shadowName in shadows:
break

print('ERROR: Make sure your spelling is correct or the shadow is an existing one...')

您应该这样编辑代码。

while shadowName not in shadows:
shadowName = input("Name of shadow: ")
if shadowName in shadows:
print("Shadow Identified...")
print(shadows[shadowName])
else:
print('ERROR: Make sure your spelling is correct or the shadow is an existing one...')

最新更新