非常简单的python函数

非常简单的python函数,python,Python,这是我想做的一个想法。函数的前3个调用是预先确定的,在第4个调用中,我想提示用户输入,我是否可以使用1个函数(不更改格式)执行此操作,因为最终的格式需要 def myfun(x,y): z=x+y Print("my x is", x) Print("my y is", y) Print("my z is", z) myfun(1,2) myfun(3,4) myfun(5,6) myfun(x,y) 任何一种方法我都可以用一个函数正确地实现这一点吗?用普通值

这是我想做的一个想法。函数的前3个调用是预先确定的,在第4个调用中,我想提示用户输入,我是否可以使用1个函数(不更改格式)执行此操作,因为最终的格式需要

def myfun(x,y):
    z=x+y
    Print("my x is", x)
    Print("my y is", y)
    Print("my z is", z)

myfun(1,2)
myfun(3,4)
myfun(5,6)
myfun(x,y)

任何一种方法我都可以用一个函数正确地实现这一点吗?

用普通值作为参数无法实现这一点,引用局部变量永远不会做任何额外的事情。但是,您可以接受提供值的函数。然后不是传递某个整数,而是传递一个返回整数的函数,而不是将
myfun
更改为执行I/O,而是传递一个执行I/O的函数

my x is 1
my y is 2
my z is 3
my x is 3
my y is 4
my z is 5
my x is 5
my y is 6
my z is 7
my x is (userinput)
my y is (userinput)
my z is ...
不过,您需要编写稍微不同的函数,因为您希望输入发生在精确的时间点。大概是这样的:

myfun(lambda: 5, lambda: 6)
# I'm gonna assume Python 3
myfun(input, input)
如果使用Python3.x,则使用input()而不是raw_input()

一行:

x = int(input('Input x: '))
y = int(input('Input y: '))
myfun(x,y)

内置函数可能对您有用。(或者,对于非3.X版本),通常不赞成将用户输入和程序逻辑组合到同一功能中。您需要的是一个控制函数,它使用股票编号、输入文件中的编号或用户提供的编号。然后调用您的
myfun
。请记住思考“我将如何测试它?”和“我能自动完成吗?”。您想让Python思考3+4=5和5+6=7吗?我不想使用你的Python版本。。。
def myfun(x=0, y=0):
   z = x + y
   Print("My x is", x)
   Print("My y is", y)
   Print("My z is", z)

myfun(1,2)
myfun(3,4)
myfun(5,6)
# here you can make a input for x and y and then you type cast the string in int
x = int(raw_input('Input x: '))
y = int(raw_input('Input y: '))
myfun(x,y)
x = int(input('Input x: '))
y = int(input('Input y: '))
myfun(x,y)
#myfun(x,y)
myfun(input("What is value of x? "),input("What is value of y? "))