Beste mede tweakers, voor een cursus moet ik een mad_libs quiz maken in python
waarin ik een tekst heb met woorden waar ik uit kan kiezen met 3 verschillende niveaus, so far so good
nu heb ik iets in elkaar kunenn flansen ( serieus Python is niet mijn ding ) :
from random import shuffle
import sys
from time import sleep
def get_word(prompt):
return raw_input(prompt).strip().lower()
def shuffled(words):
words_copy = list(words)
shuffle(words_copy)
return words_copy
class MadLib:
BLANK = "___"
DELAY = 1
def __init__(self, intros, prompt, words):
self.intros = intros
self.prompt = prompt
self.words = words
self.num_words = len(words)
assert prompt.count(self.BLANK) == self.num_words, "Number of blanks must match number of words!"
def play(self):
# shows intro
for s in self.intros:
sleep(self.DELAY)
print(s)
# displays madlib with empty fields
sleep(self.DELAY)
print(self.prompt)
# awaits user input
print("Words available: " + ", ".join(shuffled(self.words)))
gotten = [
get_word("Word {}: ".format(i))
for i in range(1, self.num_words + 1)
]
# evaluation
num_right = sum(g == w for g,w in zip(gotten, self.words))
if num_right == self.num_words:
print("Good job! Don't get cocky kid!")
return True
else:
print("You got {} out of {} words right.".format(num_right, self.num_words))
return False
madlibs = {
"padawan":
MadLib(
[
"Engage Hyperdrive!",
"Here's your quote. This should be simple.",
"Just fill in the blanks!"
],
(
"Lord ___, I should have known."
"Only you could be so ___. The imperial ___ will not sit still "
"for this, when they hear you've ___ a diplomatic..."
),
['vader', 'bold', 'senate', 'attacked']
),
"knight":
MadLib(
[
"Good choice. Lets see how much you know about Episode IV",
"Here's your text. it is a bit harder",
"Just fill in the blanks!"
],
(
"THREEPIO: He says he's the ___ of Obi-Wan Kenobi, a resident of these parts. And it's a private message for him. Quite frankly, sir, I don't know what he's talking about. Our last master was Captain Antilles, but with what we've been through, this little R2 unit has become a bit ___. "
"LUKE: Obi-Wan Kenobi? I wonder if he means old Ben Kenobi? "
"THREEPIO: I beg your pardon, sir, but do you know what he's talking about? "
"LUKE: Well, I don't know anyone named Obi-Wan, but old Ben lives out beyond the ___ sea. He's kind of a strange old hermit."
),
['property', 'eccentric', 'dune']
),
"master":
MadLib(
[
"Strong the Force with you is",
"Good Luck!",
"Here's your text:"
],
(
"OWEN: I have no need for a ___ droid. "
"THREEPIO: Sir - not in an environment such as this - that's why I've also been programmed for over thirty secondary functions that... "
"OWEN: What I really need is a droid that understands the ___ languages of moisture vaporators. "
"THREEPIO: Vaporators! Sir - My first job was programming binary load lifters... very similar to your vaporators. You could say... "
"OWEN: Do you speak ___? "
"THREEPIO: Of course I can, sir. It's like a second language for me... I'm as fluent in bocce... "
"OWEN: All right; shut up! I'll take this one. "
"THREEPIO: ___ up, sir. "
),
['protocol', 'binary', 'bocce', 'shutting']
)
}
def main():
print(
"Welcome to fill in the blanks Star Wars edition!\n"
"The point of this game is to fill in the blank spaces in the actual sentences."
)
while True:
choice = get_word("State your Jedi ranking? [padawan, knight, master] (or just hit Enter to exit) ")
if not choice:
break
elif choice in madlibs:
madlibs[choice].play()
else:
print("Sorry, that was not a recognized option.")
if __name__=="__main__":
main()
sorry for the layout... ik krijg dit niet netjes
Ik wil nadat ik een antworod heb ingevuld dat deze ook wordt ingevuld in de desbetreffende sectie, maar heb geen idee waar te beginnen...
Iemand die mij hiermee kan helpen?
waarin ik een tekst heb met woorden waar ik uit kan kiezen met 3 verschillende niveaus, so far so good
nu heb ik iets in elkaar kunenn flansen ( serieus Python is niet mijn ding ) :
from random import shuffle
import sys
from time import sleep
def get_word(prompt):
return raw_input(prompt).strip().lower()
def shuffled(words):
words_copy = list(words)
shuffle(words_copy)
return words_copy
class MadLib:
BLANK = "___"
DELAY = 1
def __init__(self, intros, prompt, words):
self.intros = intros
self.prompt = prompt
self.words = words
self.num_words = len(words)
assert prompt.count(self.BLANK) == self.num_words, "Number of blanks must match number of words!"
def play(self):
# shows intro
for s in self.intros:
sleep(self.DELAY)
print(s)
# displays madlib with empty fields
sleep(self.DELAY)
print(self.prompt)
# awaits user input
print("Words available: " + ", ".join(shuffled(self.words)))
gotten = [
get_word("Word {}: ".format(i))
for i in range(1, self.num_words + 1)
]
# evaluation
num_right = sum(g == w for g,w in zip(gotten, self.words))
if num_right == self.num_words:
print("Good job! Don't get cocky kid!")
return True
else:
print("You got {} out of {} words right.".format(num_right, self.num_words))
return False
madlibs = {
"padawan":
MadLib(
[
"Engage Hyperdrive!",
"Here's your quote. This should be simple.",
"Just fill in the blanks!"
],
(
"Lord ___, I should have known."
"Only you could be so ___. The imperial ___ will not sit still "
"for this, when they hear you've ___ a diplomatic..."
),
['vader', 'bold', 'senate', 'attacked']
),
"knight":
MadLib(
[
"Good choice. Lets see how much you know about Episode IV",
"Here's your text. it is a bit harder",
"Just fill in the blanks!"
],
(
"THREEPIO: He says he's the ___ of Obi-Wan Kenobi, a resident of these parts. And it's a private message for him. Quite frankly, sir, I don't know what he's talking about. Our last master was Captain Antilles, but with what we've been through, this little R2 unit has become a bit ___. "
"LUKE: Obi-Wan Kenobi? I wonder if he means old Ben Kenobi? "
"THREEPIO: I beg your pardon, sir, but do you know what he's talking about? "
"LUKE: Well, I don't know anyone named Obi-Wan, but old Ben lives out beyond the ___ sea. He's kind of a strange old hermit."
),
['property', 'eccentric', 'dune']
),
"master":
MadLib(
[
"Strong the Force with you is",
"Good Luck!",
"Here's your text:"
],
(
"OWEN: I have no need for a ___ droid. "
"THREEPIO: Sir - not in an environment such as this - that's why I've also been programmed for over thirty secondary functions that... "
"OWEN: What I really need is a droid that understands the ___ languages of moisture vaporators. "
"THREEPIO: Vaporators! Sir - My first job was programming binary load lifters... very similar to your vaporators. You could say... "
"OWEN: Do you speak ___? "
"THREEPIO: Of course I can, sir. It's like a second language for me... I'm as fluent in bocce... "
"OWEN: All right; shut up! I'll take this one. "
"THREEPIO: ___ up, sir. "
),
['protocol', 'binary', 'bocce', 'shutting']
)
}
def main():
print(
"Welcome to fill in the blanks Star Wars edition!\n"
"The point of this game is to fill in the blank spaces in the actual sentences."
)
while True:
choice = get_word("State your Jedi ranking? [padawan, knight, master] (or just hit Enter to exit) ")
if not choice:
break
elif choice in madlibs:
madlibs[choice].play()
else:
print("Sorry, that was not a recognized option.")
if __name__=="__main__":
main()
sorry for the layout... ik krijg dit niet netjes
Ik wil nadat ik een antworod heb ingevuld dat deze ook wordt ingevuld in de desbetreffende sectie, maar heb geen idee waar te beginnen...
Iemand die mij hiermee kan helpen?
If you put a large switch in some cave somewhere, with a sign on it saying 'End-Of-The-World Switch. PLEASE DO NOT TOUCH', the paint wouldn't even have time to dry.