Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 我知道这可以用and运算符来完成,但我想用if else语句来完成_Python 3.x - Fatal编程技术网

Python 3.x 我知道这可以用and运算符来完成,但我想用if else语句来完成

Python 3.x 我知道这可以用and运算符来完成,但我想用if else语句来完成,python-3.x,Python 3.x,对于某些特定输入,它应打印预设输出。但它不适用于第三个elif声明 operator = {"+", "-", "*", "/"} number1 = int(input("Enter number1: ")) print("Available action: ", operator) action = input("What do you want to do? &

对于某些特定输入,它应打印预设输出。但它不适用于第三个elif声明

operator = {"+", "-", "*", "/"}
number1 = int(input("Enter number1: "))
print("Available action: ", operator)
action = input("What do you want to do? ")
number2 = int(input("Enter number2: "))

if (number1 == 45):
  if (action == "+"):
    if (number2 == 3):
      print(number1, " + ", number2, " = ", 555)

elif (number1 == 56):
  if (action == "+"):
    if (number2 == 9):
      print(number1, " + ", number2, " = ", 77)

elif (number1 == 56):
  if (action == "/"):
    if (number2 == 6):
      print(number1, " / ", number2, " = ", 4)

elif (action == "+"):
  print(number1, " + ", number2, " = ", number1 + number2)

elif (action == "-"):
  print(number1, " - ", number2, " = ", number1 - number2)

elif (action == "*"):
  print(number1, " * ", number2, " = ", number1 * number2)

elif (action == "/"):
  print(number1, " / ", number2, " = ", number1 / number2)

else:
  print("Invalid Input")
这在第一个条件下可以正常工作

if (number1 == 45):
  if (action == "+"):
    if (number2 == 3):
      print(number1, " + ", number2, " = ", 555)
elif (number1 == 56):
  if (action == "+"):
    if (number2 == 9):
      print(number1, " + ", number2, " = ", 77)
elif (number1 == 56):
  if (action == "/"):
    if (number2 == 6):
      print(number1, " / ", number2, " = ", 4)
输出

Enter number1: 45
Available action:  {'*', '/', '+', '-'}
What do you want to do? +
Enter number2: 3
45  +  3  =  555
Enter number1: 56
Available action:  {'+', '-', '/', '*'}
What do you want to do? +
Enter number2: 9
56  +  9  =  77
Enter number1: 56
Available action:  {'*', '-', '/', '+'}
What do you want to do? /
Enter number2: 6
在第二种情况下也可以正常工作

if (number1 == 45):
  if (action == "+"):
    if (number2 == 3):
      print(number1, " + ", number2, " = ", 555)
elif (number1 == 56):
  if (action == "+"):
    if (number2 == 9):
      print(number1, " + ", number2, " = ", 77)
elif (number1 == 56):
  if (action == "/"):
    if (number2 == 6):
      print(number1, " / ", number2, " = ", 4)
输出

Enter number1: 45
Available action:  {'*', '/', '+', '-'}
What do you want to do? +
Enter number2: 3
45  +  3  =  555
Enter number1: 56
Available action:  {'+', '-', '/', '*'}
What do you want to do? +
Enter number2: 9
56  +  9  =  77
Enter number1: 56
Available action:  {'*', '-', '/', '+'}
What do you want to do? /
Enter number2: 6
但它不适用于第三种情况

if (number1 == 45):
  if (action == "+"):
    if (number2 == 3):
      print(number1, " + ", number2, " = ", 555)
elif (number1 == 56):
  if (action == "+"):
    if (number2 == 9):
      print(number1, " + ", number2, " = ", 77)
elif (number1 == 56):
  if (action == "/"):
    if (number2 == 6):
      print(number1, " / ", number2, " = ", 4)
第三个条件

if (number1 == 45):
  if (action == "+"):
    if (number2 == 3):
      print(number1, " + ", number2, " = ", 555)
elif (number1 == 56):
  if (action == "+"):
    if (number2 == 9):
      print(number1, " + ", number2, " = ", 77)
elif (number1 == 56):
  if (action == "/"):
    if (number2 == 6):
      print(number1, " / ", number2, " = ", 4)
  • 当number1=56时,action=“/”和number2=6。应该是打印4。 但它没有输出
输出

Enter number1: 45
Available action:  {'*', '/', '+', '-'}
What do you want to do? +
Enter number2: 3
45  +  3  =  555
Enter number1: 56
Available action:  {'+', '-', '/', '*'}
What do you want to do? +
Enter number2: 9
56  +  9  =  77
Enter number1: 56
Available action:  {'*', '-', '/', '+'}
What do you want to do? /
Enter number2: 6
它可以完美地用于其他输入。

Enter number1: 30
Available action:  {'/', '+', '-', '*'}
What do you want to do? /
Enter number2: 2
30  /  2  =  15.0

为什么它不适用于第三个
elif
语句?您可以这样做

elif number1 == 56:
    if action == "+":
        if number2 == 9:
            print(number1, " + ", number2, " = ", 77)
    elif action == "/":
        if number2 == 6:
            print(number1, " / ", number2, " = ", 4)

您可以这样做

elif number1 == 56:
    if action == "+":
        if number2 == 9:
            print(number1, " + ", number2, " = ", 77)
    elif action == "/":
        if number2 == 6:
            print(number1, " / ", number2, " = ", 4)

因为当您输入
56
时,它使用第二个branchHi,请尝试注释掉第二个“elif”块(elif(number1==56)…),并检查它是否给出了您期望的输出。如果它确实这样做了,那么请考虑您可以对“注释块”进行哪些更改,以捕获number1==56的情况,从而获得操作“+”和“/”的预期结果。最佳因为当您输入
56
时,它使用第二个分支hi,尝试注释掉第二个“elif”块(elif(number1==56)…),并检查它是否给出了您期望的输出。如果它确实这样做了,那么请考虑您可以对“注释块”进行哪些更改,以捕获number1==56的情况,从而获得操作“+”和“/”的预期结果。最好的