在python中的多个函数中使用输入变量
我想知道如何在在python中的多个函数中使用输入变量,python,function,variables,input,Python,Function,Variables,Input,我想知道如何在hello()函数的entername()函数中使用'name'变量 它是一个输入变量,有人知道如何在多个函数中使用它吗 def entername(): name = input("Please enter your name: ") return def hello(): text = "Hello " + name + ", welcome to the simulator..." for char in text: sys.
hello()
函数的entername()
函数中使用'name'变量
它是一个输入变量,有人知道如何在多个函数中使用它吗
def entername():
name = input("Please enter your name: ")
return
def hello():
text = "Hello " + name + ", welcome to the simulator..."
for char in text:
sys.stdout.write(char)
sys.stdout.flush()
char, time.sleep(0.1)
print(char)
return
不能在另一个函数中使用在函数范围内定义的变量。但是,您可以返回名称值,并将其作为参数传递给下一个函数:
import sys, time
def entername():
name = input("Please enter your name: ")
return name
def hello(name):
text = "Hello " + name + ", welcome to the simulator..."
for char in text:
sys.stdout.write(char)
sys.stdout.flush()
char, time.sleep(0.1)
print(char)
hello(entername()) # <- this chains the function calls
# and is equivalent to
# name = entername() # not the same 'name' variable as inside the entername() function, these are different scopes
# hello(name)
导入系统,时间
def entername():
name=输入(“请输入您的姓名:”)
返回名称
def你好(姓名):
text=“你好”+name+”,欢迎来到模拟器
对于文本中的字符:
系统标准输出写入(字符)
sys.stdout.flush()
字符、时间、睡眠(0.1)
打印(字符)
hello(entername())#不能在另一个函数中使用在函数范围内定义的变量。但是,您可以返回名称值,并将其作为参数传递给下一个函数:
import sys, time
def entername():
name = input("Please enter your name: ")
return name
def hello(name):
text = "Hello " + name + ", welcome to the simulator..."
for char in text:
sys.stdout.write(char)
sys.stdout.flush()
char, time.sleep(0.1)
print(char)
hello(entername()) # <- this chains the function calls
# and is equivalent to
# name = entername() # not the same 'name' variable as inside the entername() function, these are different scopes
# hello(name)
导入系统,时间
def entername():
name=输入(“请输入您的姓名:”)
返回名称
def你好(姓名):
text=“你好”+name+”,欢迎来到模拟器
对于文本中的字符:
系统标准输出写入(字符)
sys.stdout.flush()
字符、时间、睡眠(0.1)
打印(字符)
hello(entername())#如果要从不同的函数中使用相同的“name”变量,则必须在全局中定义。 在这里,我们在全局范围内定义变量,并强制函数使用全局变量,而不是定义自己的局部变量
import sys, time
name=''
def entername( ):
global name
name = input("Please enter your name: ")
return
def hello():
global name
text = "Hello " + name + ", welcome to the simulator..."
for char in text:
sys.stdout.write(char)
sys.stdout.flush()
char, time.sleep(0.1)
print(char)
return
entername()
hello()
如果要使用来自不同函数的相同“name”变量,则必须在全局中定义。 在这里,我们在全局范围内定义变量,并强制函数使用全局变量,而不是定义自己的局部变量
import sys, time
name=''
def entername( ):
global name
name = input("Please enter your name: ")
return
def hello():
global name
text = "Hello " + name + ", welcome to the simulator..."
for char in text:
sys.stdout.write(char)
sys.stdout.flush()
char, time.sleep(0.1)
print(char)
return
entername()
hello()