Python计算器编程错误

标签 python function python-3.x parameters calculator

def add(x,y):

  y = int( input("Enter First number "))
  x = int( input("Enter Second number "))
  answer = x + y
  print (answer)

def subtract(x,y):

  answer = x - y
  print (answer)
  y = int ( input("Enter First number "))
  x = int ( input("Enter Second number "))


operation = input("Subtract or Add ")

if operation == "add":

  add(x,y)

else:

  subtract(x,y)

我不断收到错误消息,提示变量 x 和 y 未被使用。请帮忙。我已经被这个问题困扰了一段时间了。

最佳答案

您的范围有问题。在调用函数之前不能调用 x 或 y,因为这些变量是在函数内部声明的。一次做一次。首先你问什么功能。然后,一旦进入函数,您就需要 x 和 y

def add():

  x = int( input("Enter First number "))
  y = int( input("Enter Second number "))
  answer = x + y
  print (answer)

def subtract():

  x = int ( input("Enter First number "))
  y = int ( input("Enter Second number "))
  answer = x - y
  print (answer)

operation = input("subtract or add ")

if operation == "add":
  add()
else:
  subtract()

关于Python计算器编程错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33048946/

相关文章:

python-3.x - 将一维数组转换为 Numpy 中的行或列向量

Python 系统日志记录器不能写入超过 2048 个字符

Python scipy fsolve "mismatch between the input and output shape of the ' func'参数”

c++ - 递归函数问题和查找所有可能的路径

c - 切换大小写菜单未执行,仅读取默认大小写

python - 如何只提取 .tar.gz 成员的文件?

python - pandas 中的总和值(如果它们按层次顺序排列)

python/excel 单元格 -> png

jquery - x 像素滚动后启动 jQuery 函数

python-3.x - 当 call tabula 遇到 Error : Cannot create file C:\Users