我有我的主模块。在主模块中,功能年龄被定义为使用户输入年龄来确定内容。这是功能的定义。
def age():
print("")
time.sleep(0.0)
playerage=int(input(""))
time.sleep(0.0)
print("Thank you.")
if playerage>=18:
ac1()
elif playerage<=18:
kc1()
return playerage
稍后我在主模块中调用可变播放器时,该功能将正常工作。我需要将变量的变量传递给其他文件中的单独模块。这两个文件位于同一根文件夹中。这是我试图将变量传递到另一个模块中的代码。
import main_module
age=main_module.age
if age>=18:
print("adult")
else:
print("child")
提前感谢对新手和学习者的帮助!
I made a few issues when editing the names of my files and such after copy and pasting to here. I tried the suggestions, and i apologize, but i didn't properly have my code represented so i want to post what it is now as i wasn't able to make it work with the suggestions since my code was a little bit different.
First, i have created a blank file names __init__.py
Here is the code from the "main_module" defining the age function.
def age():
print("some words")
time.sleep(0.0)
playerage=int(input("some words"))
time.sleep(0.0)
print("Thank you.")
if playerage>=18:
adultchoice1()
elif playerage<=18:
kidchoice1()
return playerage
That is the main file for my program. the age function is defined there with the code above. then the third_file needs the variable from age. Here is the entire test code i wrote for trying to figure this out.
#name
import time
import main_module
def dragon_opening():
print("")
age=int(main_module.age())
print(age)
if age>=18:
print("adult")
else:
print("child")
if __name__ == '__main__':
dragon_opening()
time.sleep(15.0)
Here is the code that calls third_file to run.
if playervariable==1:
print("some words")
time.sleep(2.5)
import third_file.dragon_opening
Here is the error that is generated when i run the code.
ModuleNotFoundError: No module named 'third_file.dragon_opening'; 'thrid_file' is not a package
Again my apologies to those who answered my erroneously copied code from my first post. Also a big thanks to those of you who replied to my initial post and any who post after this!
答案 0 :(得分:0)
创建一个名为
的空文件__init__.py
在您的目录中,然后使用:
from main_module import age
答案 1 :(得分:0)
欢迎来到SO,降级!
现在,您已经将变量age
分配给了函数本身,但是您需要包括方括号以实际运行它并获取其结果:
age=main_module.age()
立即尝试在print
上使用age
,您会发现它不是您所期望的。