Python 从函数返回用户提供的值
我的函数应该返回用户提供的输入。但是,每次我尝试运行这个函数并使用它之外的变量时,我都会得到一个错误,即变量未定义。我错过了什么Python 从函数返回用户提供的值,python,function,input,return,Python,Function,Input,Return,我的函数应该返回用户提供的输入。但是,每次我尝试运行这个函数并使用它之外的变量时,我都会得到一个错误,即变量未定义。我错过了什么 #function to define user input number def num_selection(): x = input("Pick a number between 1 and 10: ") return x #Run number selection num_selection() print(x) 您使用的x超出了功能范围
#function to define user input number
def num_selection():
x = input("Pick a number between 1 and 10: ")
return x
#Run number selection
num_selection()
print(x)
您使用的
x
超出了功能范围num\u选择
做一些类似于:
print(num_selection())
print(num_selection())
分配返回的值。这个
num_selection()
应该是
x = num_selection()
这样你就可以
print(x)
或直接打印结果,就像