سؤال

I have am trying to import a module into module1. I keep on getting the error : AttributeError: 'Module' object has no attribute 'poem'. The weird thing is I do have an attribute called poem.

I tried opening a file(poetry_generator.txt) or creating one depending on if it existed but I cant tell if it worked since I keep on getting the error. So if anyone can tell me in advanced that its not going to work then that would be great. Note: the code in the module poetry_generator.py does work when I run it in the file where the code exists but when I attempt to import it I get the attribute error. Please help

#Module1
import poetry_generator


myfile = open("poetry_generator.txt", "w")
myfile.write(poetry_generator.poem)
myfile.close()
print(poetry_generator.poem)

Below is the module I am importing

#poetry_generator.py

momdescription = input("word that describes your mother:")
shirt_color = input("An unsuitable colour for a t-shirt:")
smell = input("Something that smells:")
friends_name = input("A friend's name:")
wakeup = input("A word that describes how you feel when you first wake up in the morning:")
smallgreen = input("Something small and green:")
madeUpWord = input("A completely made up word:")
rude = input("A word that sounds rude, but isn't:")
fridge = input("Something you'd find in your fridge:")
uglyAnimal = input("An ugly animal:")


poem = print('''See, see the ''', momdescription ,''' sky
marvel at its big ''', shirt_color ,''' depths.
Tell me, ''', friends_name ,'''do you
Wonder why the ''', uglyAnimal ,''' ignores you?
Why its foobly stare
makes you feel ''', wakeup ,'''. 
I can tell you, it is
Worried by your ''', madeUpWord ,''' facial growth
That looks like
A ''', fridge ,'''.
What's more, it knows
Your ''', rude ,''' potting shed
Smells of ''', smallgreen ,'''.
Everything under the big ''', momdescription ,''' sky
Asks why, why do you even bother?
You only charm ''', smell ,'''s.\n''')
هل كانت مفيدة؟

المحلول

That's because the variable poem in the module you are importing from (poetry_generator) has no value associated with it. You are assigning a print function to the poem variable. And not the actual string itself.

Assigning a print function to a variable does not save the string itself. This is what I mean:

>>> poem = print("Hi")
Hi
>>> poem
>>>

You need to be doing

poem = "Hi"

Only then can you expect poetry_generator.poem to work in the other module.

Using doc strings to create output strings may not be a good choice. Create your poem variable like this instead:

poem = "See, see the , {} , sky\nmarvel at its big , {} , depths.\nTell me, , {} ,do you\nWonder why the , {} , ignores you?\nWhy its foobly stare\nmakes you feel , {} ,. \nI can tell you, it is\nWorried by your , {} , facial growth\nThat looks like\nA , {} ,.\nWhat's more, it knows\nYour , {} , potting shed\nSmells of , {} ,.\nEverything under the big , {} , sky\nAsks why, why do you even bother?\nYou only charm , {} ,s.\n".format(momdescription, shirt_color, friends_name, uglyAnimal, wakeup, madeUpWord, fridge, rude, smallgreen, momdescription, smell)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top