+ 1

Python importing of value from function in one module to other module

Hello Everyone! I would like to ask you how can I cope with this issue. How to import a variable nested in function of one module to other module? Let's say module two has a below function: def check(): race = input("1 or 2“) if race =" 1": print("human") race = "human" elif race="2": print("dwarf") race = "dwarf" Then module one: from two import check check() print(f"you are {race} ") I have problem because variable race is not visible in module one, although I can run it and provide 1 or 2 to chose the race. The problem is that then variable race seems to be not exist. If you have any idea I will be grateful for advices. Best regards

14th Dec 2019, 5:49 PM
kam kam
kam kam - avatar
2 Respostas
+ 1
Becuase you return nothing . You can modify your code like this : def check(): race = input("1 or 2“) if race =" 1": print("human") race = "human" return race elif race="2": print("dwarf") race = "dwarf" return race Then : from two import check race = check() # modified print(f"you are {race} ") and if you want to see your print use this: from two import check print(check())
14th Dec 2019, 10:27 PM
Sahand Jalali
Sahand Jalali - avatar
0
Great, thank you very much for your help:) now it's working like I've wanted:) Greetings!
15th Dec 2019, 11:32 AM
kam kam
kam kam - avatar