'Python, VSC: How to correctly import a file into my code
I'm a college student who is fairly new to both Python and VSC and am experimenting with creating my own MadLibs program. I have my main file that asks the user which story they would like, and depending on their answer, imports the correct story. Here is my main code:
#Mad Libs
#5/4/2022
num = int(input('Input a number 1-9 to choose a story!'))
if num == 1:
import MadLibs1
MadLibs1.story1
And here is my MadLibs1 code (shortened):
def story1():
verb1 = str(input('Enter an ing-verb: '))
place1 = str(input('Enter a physical location: '))
holiday_song = str(input('Enter a holiday song: '))
story = f'HOLIDAY MADNESS \n \nI was {verb1} at the {place1} the other day \
when I heard {holiday_song} come on the radio.\n\
print(story)
print('')
MadLibs1 is another python file I have, and story1 is a function that takes nouns, verbs, etc. and prints the revised mad libs story. My problem: when I run the program, it correctly asks me to input a number. When I input the number and press Enter, the following shows up:
PS C:\Users\joshu\OneDrive - University of Missouri\Documents\Python files VSC\Mad Libs>
And it expects me to provide an input. I'm not sure why it's wanting another input. It's just supposed to go straight to executing the function that is in MadLibs1.
Anyone have any idea what I am doing wrong? Thanks!
Josh
Solution 1:[1]
To import a file you need to import with the file extension. For example...
import filename.py
EDIT
In the MadLibs file you need to actually call the function. All you have done is declare the function. You call a function by typing the function name with parentheses and any required arguments. This needs to be done underneath the declared function like this
story ()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
