【发布时间】:2015-10-24 00:52:30
【问题描述】:
我遇到了一个问题,我的老师要我从另一个脚本导入函数
def readint():
prompt = int(input("Enter an integer: "))
print(" You entered: ",prompt," and the type is", type(prompt))
然后在另一个程序上我可以像这样导入它
import test
test.readint()
但是当我试图完全按照她想要的方式得到它时
test.readint(prompt)
我似乎无法让它工作。我试过了,但它似乎不起作用
def readint(prompt):
prompt = int(input("Enter an integer: "))
print(" You entered: ",prompt," and the type is", type(prompt))
return prompt
任何解释将不胜感激!
【问题讨论】:
-
您是否导入了其他脚本?另外,您似乎没有掌握函数参数的用途。
-
是的,我导入了另一个脚本,但我正在为函数而苦苦挣扎。我必须为 3 个独立的功能做这件事
-
@Austin。您缺少导入。如果
readint()在名为test.py的文件中,则在您计划使用test.readint()的文件中执行from test import *。 -
使用像
test.readint()这样的命名空间实际上需要import test,而不是from test import *。 -
@Austin:如果上述建议不起作用,您可能希望显示更多代码以及您的文件结构。