尝试提示用户输入算术运算并根据所选操作打印值

时间:2017-10-23 05:19:37

标签: python

x= int(input("Please enter the value of x: "))
y= int(input("Please enter the value of y: "))
operator = input("Please enter an operator you would like to complete the equation:")

if operator > '*':
  print("The result of the equation is: ", x*y)
elif operator > '+':
  print("The result of the equation is ", x+y)
elif operator > '-':
  print("The result of the equation is", x-y)
elif operator > '/':
  print("The result of the equation is ", x/y)

使用我的代码块,我试图将它带到操作员用户输入的位置,并根据他们输入的内容完成数学方程式,目前当我运行它时,它只会使用乘法。

1 个答案:

答案 0 :(得分:1)

您正在使用'>'检查平等操作员。在python'>'用于检查大于船舶的关系。检查平等使用' =='操作

if operator == '*':
  print("The result of the equation is: ", x*y)
elif operator == '+':
  print("The result of the equation is ", x+y)
elif operator == '-':
  print("The result of the equation is", x-y)
elif operator == '/':
  print("The result of the equation is ", x/y)