• <legend id='hP2Ze'><style id='hP2Ze'><dir id='hP2Ze'><q id='hP2Ze'></q></dir></style></legend>

    <tfoot id='hP2Ze'></tfoot>

      • <bdo id='hP2Ze'></bdo><ul id='hP2Ze'></ul>

        <i id='hP2Ze'><tr id='hP2Ze'><dt id='hP2Ze'><q id='hP2Ze'><span id='hP2Ze'><b id='hP2Ze'><form id='hP2Ze'><ins id='hP2Ze'></ins><ul id='hP2Ze'></ul><sub id='hP2Ze'></sub></form><legend id='hP2Ze'></legend><bdo id='hP2Ze'><pre id='hP2Ze'><center id='hP2Ze'></center></pre></bdo></b><th id='hP2Ze'></th></span></q></dt></tr></i><div id='hP2Ze'><tfoot id='hP2Ze'></tfoot><dl id='hP2Ze'><fieldset id='hP2Ze'></fieldset></dl></div>

        <small id='hP2Ze'></small><noframes id='hP2Ze'>

        Python中带有两个条件的if语句

        if statement with two conditions in Python(Python中带有两个条件的if语句)

          <legend id='5WkSq'><style id='5WkSq'><dir id='5WkSq'><q id='5WkSq'></q></dir></style></legend>
            <tbody id='5WkSq'></tbody>
                  <bdo id='5WkSq'></bdo><ul id='5WkSq'></ul>
                  <i id='5WkSq'><tr id='5WkSq'><dt id='5WkSq'><q id='5WkSq'><span id='5WkSq'><b id='5WkSq'><form id='5WkSq'><ins id='5WkSq'></ins><ul id='5WkSq'></ul><sub id='5WkSq'></sub></form><legend id='5WkSq'></legend><bdo id='5WkSq'><pre id='5WkSq'><center id='5WkSq'></center></pre></bdo></b><th id='5WkSq'></th></span></q></dt></tr></i><div id='5WkSq'><tfoot id='5WkSq'></tfoot><dl id='5WkSq'><fieldset id='5WkSq'></fieldset></dl></div>

                  <tfoot id='5WkSq'></tfoot>

                  <small id='5WkSq'></small><noframes id='5WkSq'>

                • 本文介绍了Python中带有两个条件的if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在编写一个简单的控制台程序来帮助自己和一些地质学同学进行岩石样本分析.我们的讲师为我们提供了一个流程图,帮助我们确定样本的特征.我正试图把它变成一个控制台程序.

                  I am writing a simple console program to help myself and some fellow geology students with rock sample analysis. Our lecturer provided us with a flow chart that helps to specify the characteristics of the sample. I am attempting to make this into a console program.

                  我的问题是第 9 行的 if 语句是否可以采用两个条件,如果可以,我是否写对了?

                  My question is whether it is possible for the if statement on line 9 to take two conditions and if so have I written it correctly?

                     def igneous_rock(self):
                      print "Welcome to IgneousFlowChart"
                      print "Assuming you are looking at an igneous rock, please choose the "
                      print "option which best describes the sample:"
                      print "1. Coherent 2. Clastic"
                  
                      choice1 = raw_input("> ")
                  
                      if choice1 = '1', 'Coherent':    # this is the line in question!
                          return 'coherent'
                      elif choice1 = '2', 'Clastic':
                          return 'clastic'
                      else:
                          print "That is not an option, sorry."
                          return 'igneous_rock'
                  

                  提前致谢:-)

                  推荐答案

                  您可以构造 if 条件应评估为 Truthy 的元素列表,然后使用 in 运算符,检查 choice1 的值是否在该元素列表中,像这样

                  You can construct the list of elements for which the if condition should evaluate to Truthy, and then use in operator like this, to check if choice1's value is in that list of elements, like this

                  if choice1 in ['1', 'Coherent']:
                  ...
                  elif choice1 in ['2', 'Clastic']:
                  ...
                  

                  除了列表,你也可以使用元组

                  Instead of lists, you can use tuples as well

                  if choice1 in ('1', 'Coherent'):
                  ...
                  elif choice1 in ('2', 'Clastic'):
                  ...
                  

                  如果要检查的项目列表很大,那么你可以像这样构造一个集合

                  If the list of items to be checked is huge, then you can construct a set like this

                  if choice1 in {'1', 'Coherent'}:
                  ...
                  elif choice1 in {'2', 'Clastic'}:
                  ...
                  

                  set 提供比列表或元组更快的查找.您可以使用 set literal 创建 sets语法 {}

                  sets offer faster lookup than lists or tuples. You can create sets with set literal syntax {}

                  这篇关于Python中带有两个条件的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                    <tbody id='Q4Fji'></tbody>
                • <small id='Q4Fji'></small><noframes id='Q4Fji'>

                  <legend id='Q4Fji'><style id='Q4Fji'><dir id='Q4Fji'><q id='Q4Fji'></q></dir></style></legend>
                    <bdo id='Q4Fji'></bdo><ul id='Q4Fji'></ul>

                        <tfoot id='Q4Fji'></tfoot>
                        <i id='Q4Fji'><tr id='Q4Fji'><dt id='Q4Fji'><q id='Q4Fji'><span id='Q4Fji'><b id='Q4Fji'><form id='Q4Fji'><ins id='Q4Fji'></ins><ul id='Q4Fji'></ul><sub id='Q4Fji'></sub></form><legend id='Q4Fji'></legend><bdo id='Q4Fji'><pre id='Q4Fji'><center id='Q4Fji'></center></pre></bdo></b><th id='Q4Fji'></th></span></q></dt></tr></i><div id='Q4Fji'><tfoot id='Q4Fji'></tfoot><dl id='Q4Fji'><fieldset id='Q4Fji'></fieldset></dl></div>