Wie trenne ich benutzerdefinierte Module von meinem Hauptprogramm in ein Unterverzeichnis, ohne dass die Fehlermeldung „Python

Python-Programme
Anonymous
 Wie trenne ich benutzerdefinierte Module von meinem Hauptprogramm in ein Unterverzeichnis, ohne dass die Fehlermeldung „

Post by Anonymous »

Ich muss also viele benutzerdefinierte Module in meine .py-Datei importieren. Wenn ich sie in einem einzigen Verzeichnis speichern könnte, wäre es viel einfacher, das Hauptprogramm zu finden.
Ich mache speziell ein Bibelquiz, weshalb ich alle diese Module benötige. Jedes Modul enthält Fragen, zum Beispiel Genesis hat 50, Exodus hat 40, Leviticus hat 27 usw. Dies ist das erste Buch, das ich noch nicht fertig habe:

Code: Select all

# Genesis.py
import random
def int_errorcheck(num):
try:
num = int(num)
return num
except:
print("You must enter an integer value!")
def check_if_right(choice):
global mychoice
global score
mychoice = input()
mychoice = int_errorcheck(mychoice)
if mychoice == choice:
print("Correct!")
score += 1
else:
print("The answer was", choice)
def question(value):
global score
if value == 1:
print('On which day of creation were amphibians created?')
print('1. Day 5')
print('2. Day 6')
print('3. Day 7')
print('4. Natrual selection')
check_if_right(2)
elif value == 2:
print("Which of these statements is true?")
print("1. The Pishon river flows through the land of Cush.")
print("2. The Tigris river flows west of Assyria.")
print("3. The Gihon river flows through the land of Cush.")
print("4. The fourth river is named the Havailah.")
check_if_right(3)
elif value == 3:
print("Why did Eve eat the forbidden fruit?")
print("1. She wanted to become like God")
print("2. The serpent deceived her")
print("3. It looked good")
print("4. All of the above")
check_if_right(3)
elif value == 4:
print("Which of these statements are true:")
print("1. Abel was older, took care of sheep")
print("2. Abel was younger, took care of crops/plants")
print("3. Cain was younger, took care of sheep")
print("4. Cain was older, took care of crops/plants")
check_if_right(4)
elif value == 5:
print("When did Noah have Shem, Ham, and Japheth?")
print("1. 500 years")
print("2. 520 years")
print("3. 540 years")
print("4. 560 years")
check_if_right(1)
elif value == 6:
print("What material was the ark made of?")
print("1. Oak wood")
print("2. Gopher wood")
print("3. Birch wood")
print("4. Spruce wood")
check_if_right(2)
elif value == 7:
print("What word did God use for Noah's family entering the Ark?")
print("1. 'You and all your household'")
print("2. 'You and all your group'")
print("3. 'You and all your family'")
print("4. 'You and all your kids'")
check_if_right(1)
elif value == 8:
print("How long did the ark stay on the water?")
print("1. 40 days")
print("2. 150 days")
print("3. 187 days")
print("4. 370 days")
check_if_right(4)
elif value == 9:
print("What does the rainbow represent?")
print("1. A promise")
print("2. LGBTQ+")
print("3. Something beautiful")
print("4. It dosen't represent anything")
check_if_right(1)
elif value == 10:
print("Which child of Noah had the most sons?")
print("1. Ham")
print("2. Japheth")
print("3. Shem")
print("4.  They all had the same number")
check_if_right(2)
else:
score += 1
def main():
input("Ready to begin?")
global score
score = 0
print('Bible Quiz')
print('----------')
for i in range(0, 50):
question(random.randrange(1, 51))
print(f"Your score was {score} out of {i + 1}.")
Mein Ziel hier ist es, die benutzerdefinierten Module von meinem Hauptprogramm zu trennen, damit das Hauptprogramm viel einfacher zu finden ist, aber wenn ich das versuche, erhalte ich einen ModuleNotFoundError:

Code: Select all

import genesis
genesis.question(random.randrange(1, 11))
Und außerdem: Wie importiere ich alle 66 meiner benutzerdefinierten Module, um alle Bibliotheken zu bereinigen, ohne dass der Code so aussieht:

Code: Select all

import genesis
import exodus
import leviticus
import numbers
import deuteronomy
import joshua
import judges
# etc...

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post