[英]How can I use existing variables and their values as parameters for a function (Python)?
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: 因此,如果您输入数字4:
Please enter a number
>>> 4
Your number plus two is 6
That's all I need and hopefully it is simple! 这就是我所需要的,希望它很简单! Thanks!
谢谢!
As @BrenBarn suggested 正如@BrenBarn建议的
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
. 您只需将参数传递给您的函数即可,该函数只需要一个参数
number
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.