Python 尝试应用导入的模块后发生NameError

Python 尝试应用导入的模块后发生NameError,python,import,nameerror,Python,Import,Nameerror,我有一个稍微冗长且重复的程序,我将其他模块导入其中,然后将收集到的所有信息放入一个完整的句子中。我遇到的问题是在进入下一部分之前定义的所有内容,显示为名称错误 代码如下: import number print("Hello! \nWhat is your name?") myName = input() print("Well, " + myName + ", I think it is time for us to play a little game.") print("

我有一个稍微冗长且重复的程序,我将其他模块导入其中,然后将收集到的所有信息放入一个完整的句子中。我遇到的问题是在进入下一部分之前定义的所有内容,显示为名称错误

代码如下:

 import number 
 print("Hello! \nWhat is your name?") 
 myName = input()
 print("Well, " + myName + ", I think it is time for us to play a
 little game.") 
 print("First, I need to know how old you are. Please
 type your age using only numbers.") 
 while True:
     age = input()
     try:
         if age:
             age = float(age)
             print("Great!\nNow, where do you live " + myName + "?")
             import Place
     except ValueError:
         print("I'm sorry, I did not understand your answer. Please only use digits and no decimals.")
以下是Place模块:

 print("As a reminder, I am unable to tell the difference between
 places and anything else you respond with. You can make me sound
 silly, or you can just answer the question so everything makes sense
 in the end!") 
 place = input() 
 print("Alright!\nNow what is your
 gender?") 
 print("While today's society has more than two ways to
 describe gender, please only use male or female for the sake of
 simplicity!") 
 while True:
     gender = input()
     if gender == "male":
         print("Your name is " + myName + " and you are " + age + " years old. You are a " + gender + " and you live in " + place + "!")
         import Answer
     if gender == "MALE":
         print("Your name is " + myName + " and you are " + age + " years old. You are a " + gender + " and you live in " + place + "!")
         import Answer
     if gender == "Male":
         print("Your name is " + myName + " and you are " + age + " years old. You are a " + gender + " and you live in " + place + "!")
         import Answer
     if gender == "FEMALE":
         print("Your name is " + myName + " and you are " + age + " years old. You are a " + gender + " and you live in " + place + "!")
         import Answer
     if gender == "Female":
         print("Your name is " + myName + " and you are " + age + " years old. You are a " + gender + " and you live in " + place + "!")
         import Answer
     if gender == "female":
         print("Your name is " + myName + " and you are " + age + " years old. You are a " + gender + " and you live in " + place + "!")
         import Answer
     else:
         print("Are you a male or female?")
以下是答案模块:

while True:
    print("Did I get everything correct?\nPlease say yes or no.")
    answer = input()
    if answer == "Yes":
        print("Great! Thanks for playing!")
        break
    if answer == "yes":
        print("Great! Thanks for playing!")
        break
    if answer == "YES":
        print("Great! Thanks for playing!")
        break
    elif answer == "no":
        print("Okay! To make sure I avoid any errors, we must start from the beginning!")
        import Self_Story
    elif answer == "No":
        print("Okay! To make sure I avoid any errors, we must start from the beginning!")
        import Self_Story
    elif answer == "NO":
        print("Okay! To make sure I avoid any errors, we must start from the beginning!")
        import Self_Story
    else:
        print("I'm sorry, I did not understand that.")
以下是错误消息:

Traceback (most recent call last):
  File "/Users/Maddiefayee/Documents/Self_Story.py", line 12, in <module>
    import Place
  File "/Users/Maddiefayee/Documents/Place.py", line 20, in <module>
    print("Your name is " + myName + " and you are " + age + " years old. You are a " + gender + " and you live in " + place + "!")
NameError: name 'myName' is not defined
回溯(最近一次呼叫最后一次):
文件“/Users/Maddiefayee/Documents/Self_Story.py”,第12行,在
进口地
文件“/Users/Maddiefayee/Documents/Place.py”,第20行,在
打印(“你的名字是“+myName+”,你是“+age+”岁。你是“+gender+”,你住在“+place+”!”)
名称错误:未定义名称“myName”

这是因为当你导入一些东西时,变量不会遗留下来。相反,你应该这样说:

def place():
    print("As a reminder, I am unable to tell the difference between
places and anything else you respond with. You can make me sound
silly, or you can just answer the question so everything makes sense
in the end!") 
place = input() 
print("Alright!\nNow what is your
gender?") 
print("While today's society has more than two ways to
describe gender, please only use male or female for the sake of
simplicity!") 
while True:
    gender = input()
    if gender == "male":
        print("Your name is " + myName + " and you are " + age + "    years old. You are a " + gender + " and you live in " + place + "!")
     import Answer
 if gender == "MALE":
     print("Your name is " + myName + " and you are " + age + " years old. You are a " + gender + " and you live in " + place + "!")
     import Answer
 if gender == "Male":
     print("Your name is " + myName + " and you are " + age + " years old. You are a " + gender + " and you live in " + place + "!")
     import Answer
 if gender == "FEMALE":
     print("Your name is " + myName + " and you are " + age + " years old. You are a " + gender + " and you live in " + place + "!")
     import Answer
 if gender == "Female":
     print("Your name is " + myName + " and you are " + age + " years old. You are a " + gender + " and you live in " + place + "!")
     import Answer
 if gender == "female":
     print("Your name is " + myName + " and you are " + age + " years old. You are a " + gender + " and you live in " + place + "!")
     import Answer
 else:
     print("Are you a male or female?")
def answer():
   while True:
   print("Did I get everything correct?\nPlease say yes or no.")
   answer = input()
   if answer == "Yes":
       print("Great! Thanks for playing!")
       break
   if answer == "yes":
       print("Great! Thanks for playing!")
       break
   if answer == "YES":
       print("Great! Thanks for playing!")
       break
   elif answer == "no":
       print("Okay! To make sure I avoid any errors, we must start from    the beginning!")
       import Self_Story
   elif answer == "No":
       print("Okay! To make sure I avoid any errors, we must start from    the beginning!")
       import Self_Story
   elif answer == "NO":
       print("Okay! To make sure I avoid any errors, we must start from    the beginning!")
       selfStory()
   else:
       print("I'm sorry, I did not understand that.")
def selfStory():
   import number 
   print("Hello! \nWhat is your name?") 
   myName = input()
   print("Well, " + myName + ", I think it is time for us to play a
   little game.") 
   print("First, I need to know how old you are. Please
   type your age using only numbers.") 
   while True:
       age = input()
       try:
           if age:
               age = float(age)
               print("Great!\nNow, where do you live " + myName + "?")
               place()
       except ValueError:
           print("I'm sorry, I did not understand your answer. Please    only use digits and no decimals.")
selfStory()
我会将您的“模块”转换为函数,并将它们全部放在同一个模块中(这真的不是一个很大的程序,我也不认为有任何理由为此使用单独的模块)。然后,您可以运行此模块,整个程序将按预期运行:

def GetNameAge():
    print("Hello! \nWhat is your name?") 
    myName = input()
    print("Well, " + myName + ", I think it is time for us to play a little game.") 
    print("""First, I need to know how old you are. Please
    type your age using only numbers.""") 
    while True:
        age = input()
        try:
            if age:
                age = str(float(age))
                return myName, age
        except ValueError:
            print("I'm sorry, I did not understand your answer. Please only use digits and no decimals.")

def GetPlaceGender():
    print("""As a reminder, I am unable to tell the difference between
    places and anything else you respond with. You can make me sound
    silly, or you can just answer the question so everything makes sense
    in the end!""") 
    place = input() 
    print("""Alright!\nNow what is your gender?\nWhile today's society has more than two ways to describe gender, 
    please only use male or female for the sake of simplicity!""") 
    while True:
        gender = input()
        gender = gender.lower().strip()
        if gender in ["male","female","m","f"]:
            return "male" if gender[0] == "m" else "female", place
        else:
            print("Are you a male or female?")   

def GetAnswer():
    while True:
        print("Did I get everything correct?\nPlease say yes, no, or exit.")
        answer = input()
        answer = answer.lower().strip()
        if answer in ["yes","y"]:
            print("Great! Thanks for playing!")
            return True
        elif answer in ["no","n"]:
            print("Okay! To make sure I avoid any errors, we must start from the beginning!")
            return False
        elif answer == "exit":
            return True
        else:
            print("I'm sorry, I did not understand that.")

if __name__ == "__main__":    
    while True:
        myName, age = GetNameAge()
        print("Great!\nNow, where do you live, " + myName + "?")
        gender, place = GetPlaceGender()
        print("Your name is " + myName + " and you are " + age + " years old. You are a " + gender + " and you live in " + place + "!")
        if GetAnswer(): break

由于这是张贴我相信我弄乱了格式张贴的代码,道歉,仍然是真的在这个新的!请修正你的格式。单击编辑,粘贴代码,然后按Control+K(或Cmd+K)。当你在做的时候,请发布一个MVCE。你到底在哪一行得到了错误?另外,你能粘贴错误消息吗?如果你做
gender=input().lower().strip()
,你可以减少很多重复,因为任何“Male”或“Male”或“Male”等的大小写都会转换成小写
“Male”
…同样,如果“应答模块与其余代码无关”,那你为什么要导入它呢?