How can I use existing variables and their values as parameters for a function (Python)?

点点圈 提交于 2019-12-11 18:47:13

问题


I believe what I am asking is pretty simple, however none of my searches have turned up useful information.

So, all I want is to be able to access a preexisting variable in a program and use its value as a parameter of one of my own functions.

print("Please enter a number")
number = int(input())

def addTwo(number):
    newNumber = number + 2
    print("Your number plus two is ", str(newNumber))

So if you entered the number 4:

Please enter a number
>>> 4
Your number plus two is 6

That's all I need and hopefully it is simple! Thanks!


回答1:


As @BrenBarn suggested

print("Please enter a number")
number = int(input())

def addTwo(number):
    newNumber = number + 2
    print("Your number plus two is ", str(newNumber))

addTwo(number)

You simply pass parameters to your function which takes exactly one argument number.



来源:https://stackoverflow.com/questions/24771265/how-can-i-use-existing-variables-and-their-values-as-parameters-for-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!