python - 在导入函数 python 中使用用户生成的变量

标签 python function python-import

我不知道该怎么调用它,所以我只是解释一下我需要做什么

假设我有以下脚本来获取用户输入:

#test0.py

import functions

number = input('pick a number')
functions.dothis()

还有这个:

#functions.py
def dothis(): 
    global number
    if number == 1:
        print('the number is 1')
    else: 
        print('the number is not one')

这显然是射出的'number' is not defined

如何通过number进入dothis()功能还是我完全以错误的方式处理这个问题?

干杯!

最佳答案

只需将其作为参数传递,如下所示:

def dothis(number): 
    if number == 1:
       ...

number = input('pick a number')
functions.dothis(number)

关于python - 在导入函数 python 中使用用户生成的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41455849/

相关文章:

python - 基于行值的seaborn热图配色方案

python - 将列中的所有唯一单词放入新数据集中

C++ 函数数组

未找到 Python 模块,相对文件夹。想不通

python - 如何在 Python zeep 中指定 SOAP 服务器 URL

javascript - 检查按钮 "hasn' t"是否在一定时间内被点击 (jQuery)

mysql - 在 MySQL 中创建一个用户定义的函数来处理 auto_increment

python - pip install -e,Python 路径和命名空间包

python - 如何处理 Python 模块中的冗余/重复导入?

python - 如何使嵌入式Python解释器的本地空间与全局空间共享一些变量