【问题标题】:python multiple conditions at the same timepython同时多个条件
【发布时间】:2015-12-15 19:18:13
【问题描述】:

我是 python 2.7 的基本用户。我的查询是这样的

x = raw_input(...:)

if len(x) != 6

    re enter x

if x(1:2) < 0 or x(1:2) > 20

    re enter x 

if x(1:2) < 0 or x(1:2) > 20

    re enter x  

上述过程的问题在于它对条件一一检查。例如,如果在第三个 if 我输入 4 位而不是 6 位,它仍然是一个错误,但不会显示正确的错误。

我尝试在 if 语句所在的开头使用 while 循环,但出现了同样的问题:它捕获了错误但没有捕获正确的错误消息。

我非常感谢您对此的任何帮助。我想要的是,无论我在哪里重新输入x,它都应该检查所有的 if 语句。

【问题讨论】:

  • x(1:2) 看起来不像是有效的语法......我错过了什么吗?通常,如果您发布我们可以运行的实际代码并查看您描述的行为,我们可以为您提供最好的帮助。
  • 请尝试在此处发布之前执行代码,这样您就可以看到会发生什么,而不是要求我们为您预测。
  • 对不起,如果我有点直言不讳。
  • import string x = raw_input("输入数字:") a =str(x) if len(x) != 6: input= raw_input("no必须有6位数字:" ) if (a[0:2]'12'): input= raw_input("前2位必须是1-12:") if (a[3 :5]'35'): input= raw_input("中间2位必须是1-35:") print 'hello'
  • 我要重新发布这个抱歉

标签: python loops


【解决方案1】:

从非常模糊的请求中,我只能提取 4 件事;

你想要:

  1. 应重新提示输入 x,直到 x 满足条件。
  2. x 的长度应该不同于 6?
  3. 如果 x 是 123456,您希望 2 大于 0 且小于 20?

    while True: #This is a while loop. True could be replaced with a boolean. But for now, we will keep it True to run until break. 
        x=raw_input() #Get some input
        if len(x) != 6 or 0<x[1:2]<20: #I used or because I don't know if you want both to be True, or 1 to be True, in order for the input to be invalid. Switch to and for both.      
            print "wrong please try again" 
            continue #Continue takes the code back to the beginning where we prompted for input. 
        else: #If the else is not satisfied...
            break #Break out of the loop and stock asking for input. 
    

你应该从自己的代码中学到一些东西:

x = raw_input(...:)

if len(x)!=6 #This one I understand, but still, should use while loop.

 re enter x #Unneccesary if you switch to while loop and use keyword continue. 

if x(1:2)<0 or x(1:2)>20 #x(1:2) I am guessing you want to slice, you need [] not ()

re enter x #Unneccesary if you switch to while loop and use keyword continue.

if x(1:2)<0 or x(1:2)>20 #Why do you even have this here? Same as line above

re enter x  #Unneccesary if you switch to while loop and use keyword continue.

#Did you want your conditions to BOTH be True and reenter, or only 1? 
#This is important for boolean logic. 
#This is what determines if you will use AND or OR boolean operators. 

【讨论】:

    【解决方案2】:

    只需使用if...else 代码:

    if condition_1:
        ...do something...
    elif condition 2:
        ...do something else...
    else:
        ...do something else again...
    

    每次用户输入内容后,您都必须检查这些条件。

    另外,您可以使用变量来检测 valir 输入:

    valid = False
    while valid == False:
        valid = True
        input = raw_input()
        if error_condition:
            valid = False
            ...do_something...
    

    【讨论】:

      【解决方案3】:

      使用any:

      while True:
          x = raw_input(prompt)
          if any([len(x) != 6, x[1:2] < 0,  x[1:2] > 20]):
              continue
          break
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-31
        • 2015-12-11
        • 1970-01-01
        • 1970-01-01
        • 2021-05-29
        相关资源
        最近更新 更多