Python:当 False 时继续执行脚本的 if/elif 函数?

标签 python function if-statement dictionary return

编辑:

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

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

如果用户说"is",则一切正常。

但是,如果用户回答“否”,则会再次询问第一个问题(大概是因为我使用 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']  

最佳答案

您需要像这样更改条件:

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

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

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

关于Python:当 False 时继续执行脚本的 if/elif 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29199070/

相关文章:

c# - 无法从 aspx 页面获取值到 aspx.cs

javascript - 为什么 JavaScript getTime() 不是一个函数?

Python 点击​​ : access option values globally

python - python 中 strftime 的基本日期解析用法

python - 比较响应数据的单元测试引发 TypeError

javascript - 单击网格特定数据索引上的事件 - Ext JS

r - R 中的嵌套 if-else 循环

excel - 如何将 1 添加到 Excel 中最近的重复行?

java - 查找某个项目在数组中出现的次数

python - Request.data 或 Request.query_params 来访问 POST 中的参数?