如果if语句等于字典的值并打印字典值,我想返回if语句,而不仅仅是使用=="HW"并打印一些文本。现在我什么也不回所以我想知道我做错了什么。当我先给出错误答案,然后给出正确答案时,我如何摆脱while循环?
from ex45f import live
class adult(live):
def __init__(self, choose):
self.choose = choose
def choices(self):
options = {
'HW': 'Hard Working',
'PA': 'Partying',
'DE': 'Doing everyting a bit',
}
#while choose != 'HW' or 'PA' or 'DE':
while not (self.choose == 'HW' or self.choose == 'PA' or self.choose == 'DE'):
x = input("""Choose again
> """)
print(x)
if self.choose == options['HW']:
return "You are going to be millionare"
elif self.choose == options['PA']:
return "You will have the first year a great life and then you will hate it"
elif self.choose == options['DE']:
return "Nothing intersting in life happens."
else:
return "Wrong input"
choose = input("""Choose one of those options: HW, PA, DE)
> """)
zw = adult(choose)
zw.choices()
所以有几个评论:
-
self-choose将是HW、PA或DE如果州政府官员检查
if self.choose == options['HW']
。
options[‘HW’]实际上是";努力工作">,因此在上述情况下,self.choose将始终在else
结束
您的if语句应该是:if self.choose == "HW":
if self.choose == "PA":
if self.choose == "DE":
-
您的
while
循环可能看起来像:while self.choose not in options:
-
如果self.choose不在列表中,则获得一个新输入并将其存储在x中。
self.choose
保持不变。因此while循环将是无限的
使用self.choose = input
而不是x = input
,因此当用户输入正确的选项时,他们将离开while循环。 -
如果要获得
options
的值,请添加return "{} The text to return".format(options[self.choose])
-
最后,如果您对术语
Hard Working
、Partying
或Doing everthing a bit
不感兴趣,那么只需列出选项即可:options = ["DE", "PA", "HW"]
然后您就不需要options[self.choose]
我希望这能有所帮助!