【问题标题】:Python: if/elif function that continues through script when False? [duplicate]Python:如果/elif函数在False时继续通过脚本? [复制]
【发布时间】:2015-03-22 19:48:51
【问题描述】:

编辑:

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

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

如果用户说“是”,则一切正常。

但是,如果用户回答“否”,则会再次询问第一个问题(可能是因为我使用 raw_input() 的 if/elif 结构),然后跳过完成脚本的所有其他问题。

如何构建它,以便如果用户对第一个问题说“不”,它会问下一个问题而不是每个问题两次?

打印输出:

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']  

【问题讨论】:

  • or "yes" 表示如果 "yes" == True 并且 "yes" 始终为 True
  • 我应该为每个问题使用单独的函数吗?

标签: python function if-statement dictionary return


【解决方案1】:

你需要像这样改变你的条件:

raw_input(questions["strong"]) in ("Yes", "yes")

或者,您可以尝试检查一种情况:

raw_input(questions["strong"]).lower() == "yes"

【讨论】:

    猜你喜欢
    • 2016-09-04
    • 1970-01-01
    • 2011-11-24
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 2021-08-30
    相关资源
    最近更新 更多