Python:if/elif函数,当为False时通过脚本继续?

2024-04-20 15:19:29 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑:

我从字典中问用户一组问题:

questions = {
  "strong": "Do ye like yer drinks strong? ",
  "salty": "Do ye like it with a salty tang? ",
  "bitter": "Are ye a lubber who likes it bitter? ",
  "sweet": "Would ye like a bit of sweetness with yer poison? ",
  "fruity": "Are ye one for a fruity finish? "
}

这些键与另一个字典相关联:

ingredients = {
  "strong": ["glug of rum", "slug of whiskey", "splash of gin"],
  "salty": ["olive on a stick", "salt-dusted rim", "rasher of bacon"],
  "bitter": ["shake of bitters", "splash of tonic", "twist of lemon peel"],
  "sweet": ["sugar cube", "spoonful of honey", "splash of cola"],
  "fruity": ["slice of orange", "dash of cassis", "cherry on top"]
}

我通过为每个问题设置的“简单”if/elif来问他们问题,并将他们的答案分配到一本新字典中:

beverage = {}


def drink():
  """Find out user preferences and assign to new dictionary"""
  if raw_input(questions["strong"]).lower() == "yes":
    beverage["strong"] = ingredients["strong"]
  elif raw_input(questions["strong"]).lower() == "no":
    return False
  if raw_input(questions["salty"]).lower() == "yes":
    beverage["salty"] = ingredients["salty"]
  elif raw_input(questions["salty"]).lower() == "no":
    return False

...


drink()

最后打印饮料:

print beverage

如果用户说“是”,一切正常。你知道吗

但是,如果用户回答“否”,第一个问题将再次被询问(可能是因为我的if/elif结构使用了raw\u input()),然后跳过所有其他问题来完成脚本。你知道吗

我该如何构造它,以便如果用户对第一个问题说“不”,那么它会问下一个问题,而不是每个问题问两次?你知道吗

打印输出:

Do ye like yer drinks strong? yes                                                                                                                                            
Do ye like it with a salty tang? no                                                                                                                                          
Do ye like it with a salty tang? yes                                                                                                                                         
Are ye a lubber who likes it bitter? no                                                                                                                                      
Are ye a lubber who likes it bitter? yes                                                                                                                                     
Would ye like a bit of sweetness with yer poison? no                                                                                                                         
Would ye like a bit of sweetness with yer poison? yes                                                                                                                        
Are ye one for a fruity finish? no                                                                                                                                           
Are ye one for a fruity finish? yes                                                                                                                                          
Yer cocktail is a made of a                                                                                                                                                  
['glug of rum']  

Tags: ofnowithitdoarelikeyes