Ich wollte mir mal einen "Vokabeltrainer" mit Python schreiben. Auf die Idee bin ich durch den Code hier gekommen:
Code: Alles auswählen
import random
from urllib import urlopen
import sys
WORD_URL = "http://learncodethehardway.org/words.txt"
WORDS = []
PHRASES = {
"class %%%(%%%):":
"Make a class named %%% that is-a %%%.",
"class %%%(object):\n\tdef __init__(self, ***)" :
"class %%% has-a __init__ that takes self and *** parameters.",
"class %%%(object):\n\tdef ***(self, @@@)":
"class %%% has-a function named *** that takes self and @@@ parameters.",
"*** = %%%()":
"Set *** to an instance of class %%%.",
"***.***(@@@)":
"From *** get the *** function, and call it with parameters self, @@@.",
"***.*** = '***'":
"From *** get the *** attribute and set it to '***'."
}
# do they want to drill phrases first
if len(sys.argv) == 2 and sys.argv[1] == "english":
PHRASE_FIRST = True
else:
PHRASE_FIRST = False
# load up the words from the website
for word in urlopen(WORD_URL).readlines():
WORDS.append(word.strip())
def convert(snippet, phrase):
class_names = [w.capitalize() for w in
random.sample(WORDS, snippet.count("%%%"))]
other_names = random.sample(WORDS, snippet.count("***"))
results = []
param_names = []
for i in range(0, snippet.count("@@@")):
param_count = random.randint(1,3)
param_names.append(', '.join(random.sample(WORDS, param_count)))
for sentence in snippet, phrase:
result = sentence[:]
# fake class names
for word in class_names:
result = result.replace("%%%", word, 1)
# fake other names
for word in other_names:
result = result.replace("***", word, 1)
# fake parameter lists
for word in param_names:
result = result.replace("@@@", word, 1)
results.append(result)
return results
# keep going until they hit CTRL-D
try:
while True:
snippets = PHRASES.keys()
random.shuffle(snippets)
for snippet in snippets:
phrase = PHRASES[snippet]
question, answer = convert(snippet, phrase)
if PHRASE_FIRST:
question, answer = answer, question
print question
raw_input("> ")
print "ANSWER: %s\n\n" % answer
except EOFError:
print "\nBye"

Wollte den jetzt bisschen umschreiben damit ich damit meine Vokabeln machen kann. Hab nun aber das Problem, dass ich 2 Variablen (Deutsche & Englische Vokabeln) habe. Und nicht darauf komme wie ich es mache, dass der rafft welche Vokabeln zusammen gehören (wär ja blöd wenn ich z.B. Haus - house, Regen - rain als Vokabeln habe und der dann house abfragt und meint, dass die Antwort Regen wär ^^).
Hier ist mal mein Code, soweit bin ich mitlerweile, hab auch schon vieles ausprobiert aber bekomme entweder nen Fehler, dass es nicht geht oder er zeigt für die @@@ die %%% an oder bleibt bei den @@@ :/ (und ich hab da jetzt noch nichts wo er die Variable WORDS_deutsch aufruft, weil ich keine Idee habe wo das hin sollte).
Bei dem Code (oben) ist es ja so, dass er 2 Lücken hat, die mit 2 zufälligen Wörtern gefüllt werden und dann sind auch die selben in der Antwort. Bei mir müsste es aber so sein, dass er 2 Wörter hat (die auch zusammen gehören also Haus - house und NICHT Haus - rain^^) und, dass er in der Frage nur eins hin schreibt und in der Antwort dann das 2.
Code: Alles auswählen
# -*- coding: utf-8 -*-
import random
import sys
WORDS_deutsch = [
"einen Satz prägen",
"Glaube",
"überdauern",
"das Streben nach Glück",
"Erreichbarkeit",
"Anstrengung",
"Siedlungsgrenze, Grenzland,",
"Unabhängigkeit",
"Anpassung",
"Erfolg haben",
"Expansion",
"Vorsehung",
"erobern",
"territoriales Wachstum",
"das Erbe",
"Selbstvertrauen",
"gedeihen",
"versagen",
"Chancengleichheit",
"vorankommen",
"Reichtum",
"konkurrieren",
"Wettbewerb",
"sich verlassen auf",
"Besitz, Eigentum",
"in hohem Maße",
"garantieren",
"Erwartung",
"Wert",
"Schicksal",
"Schicksal (negativ besetzt)",
"ein Mangel an",
"herleiten von",
",offenkundiges Schicksal'",
"vom Tellerwäscher zum Millionär"
]
WORDS_englisch = [
"to coin a phrase",
"belief, beliefs",
"to endure",
"the pursuit of happiness",
"attainability",
"endeavour",
"frontier",
"independence",
"assimilation",
"to succeed",
"expansion",
"providence",
"to conquer",
"territorial growth",
"heritage",
"self-reliance",
"to prosper",
"to fail",
"equality of opportunity",
"to advance",
"wealth",
"to compete with",
"competition",
"to rely on",
"possession",
"to a great extent",
"to ensure",
"expectation",
"value",
"destiny",
"fate",
"a lack of",
"to derive from",
"manifest destiny",
"from rags to riches"
]
PHRASES = {
"English: \n %%%" : "\n@@@",
}
def convert(snippet, phrase):
class_names = [w.lower() for w in
random.sample(WORDS_englisch, snippet.count("%%%"))]
results = []
for sentence in snippet, phrase:
result = sentence[:]
# fake class names
for word in class_names:
result = result.replace("%%%", word, 1)
results.append(result)
return results
# keep going until they hit CTRL-D
try:
while True:
snippets = PHRASES.keys()
random.shuffle(snippets)
for snippet in snippets:
phrase = PHRASES[snippet]
question, answer = convert(snippet, phrase)
print question
raw_input("> ")
print "ANSWER: %s\n\n" % answer
except EOFError:
print "\nBye"
hab noch die Vermutung, dass mit nem Dictionary Python sagen könnte welche Vokabeln zu einander gehören, dann ist aber noch das Problem mit dem richtigen Anzeigen :/
Ist bestimmt der größte Müll der da steht aber was solls aus Fehlern lernt man (hoffentlich).
Geht das überhaupt mit der "Basis" von dem ersten Code? Wenn ja wie?
oder braucht man da was komplett anderes? wenn ja was?
Hoffe ihr könnt mir helfen

MFG