def rec():
usr = input('Enter Name : ')
pwd = input('Enter Password : ')
return usr , pwd
def show():
c = rec()
print(c.usr , c.pwd)
show()
——错误打印(c。用户,c.pwd)AttributeError: 'NoneType'对象没有属性'usr'
这只是一个例子,我想知道我们如何将一个def函数的值传递给另一个def函数,就像我在defrec()中获取值并在def show()中显示值ps:你能告诉我如何在评论中做到这一点,因为我只是初学者,提前感谢
你可以这样做,但要做到这一点,你需要从你调用的函数返回一些东西。
def rec():
usr = input('Enter Name : ')
pwd = input('Enter Password : ')
return 10 # just an example to show that you need to return something here so that `c` can receive that value
def show():
c = rec() # `c` will receive the value that you return from `rec()`
print(c.usr , c.pwd)
show()
对于你的问题,代码应该是这样的:
def rec():
usr = input('Enter Name : ')
pwd = input('Enter Password : ')
return usr, pwd # you returned two values
def show():
c, d = rec() # as you returned two values, you also need to receive them
print(c , d) # you values are in `c` and `d`, and there is nothing like `c.usr` here, so use only `c` and `d`
show()
输出:
Enter Name : shahin
Enter Password : 123
shahin 123