我如何使用现有变量及其值作为函数的参数(Python)? [英] How can I use existing variables and their values as parameters for a function (Python)?

查看:160
本文介绍了我如何使用现有变量及其值作为函数的参数(Python)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我所要求的很简单,但是我的搜索都没有提供有用的信息。

所以,我想要的只是能够访问一个在程序中预先存在的变量,并使用它的值作为我自己函数的一个参数。

  print(请输入数字)
数字= int(输入())

def addTwo(数字):
newNumber =数字+ 2
print(您的电话号码加两,str(newNumber))

所以如果你输入数字4:

 请输入一个数字
>>> 4
您的电话号码加上2就是6

这就是我需要的,希望它很简单!谢谢!

解决方案

正如@BrenBarn建议的

  print(请输入一个数字)
number = int(input())

def addTwo(number):
newNumber = number + 2
print(你的号码加两是str(newNumber))

addTwo(号码)

只需将参数传递给函数,该函数只需要一个参数 number


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!

解决方案

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.

这篇关于我如何使用现有变量及其值作为函数的参数(Python)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆