Created
May 4, 2013 18:31
-
-
Save horstjens/5518306 to your computer and use it in GitHub Desktop.
quiz for dennis with functions, while loops and an for loop ( iterating over a list of questions )
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# python3 | |
# hi dennis. first, let us define parts that repeat themself a lot | |
# programmers hate typing the same thing over and over and prefer | |
#to define (def) function and call them over and over ... it's less typing | |
# let's define a function that simply prints the different possibilitys for the user | |
# ask the user for an answer, checks the answer (ask again if answer was invalid) | |
# and return the correct answer. (0,1,2 or 3). Note that those answers are strings. | |
def ask_how_much_you_agree(): | |
'''displays help text and ask the user for one of those possible choices: | |
0,1,2,3. Ask again if answer is invalid. returns correct answer as string''' | |
while True: # never ending loop begins, because True is always True | |
print ("type 0 = this doesn't apply to me") | |
print ("type 1 = there is some truth to this") | |
print ("type 2 = I agree") | |
print ("type 3 =strongly agree") | |
answer = input("Your answer please ( press number and ENTER ):") | |
if answer in ["0","1","2","3"]: | |
break # the answer was valid, escape out of the endless while loop | |
print ("i will ask until you give an valid answer") # and back to the start of the while loop | |
# the while-loop was sucessfully escaped... | |
return answer | |
# the second part that always repeat itself is asking the user for an detailed answer. let's also | |
# write a function for it | |
def ask_details(): | |
'''ask for details until the user enters at least something (even a single char). returns the details | |
as string.''' | |
while True: # never ending loop begins | |
answer = input("Why do you say that? Please write some details and press Enter:") | |
if len(answer) > 0: | |
break # escape out of the loop | |
print("if you just hit enter, i just ask again...") | |
# the while loop was sucessfully escaped | |
return answer | |
### now let's make a big list of the possible questions. | |
# ( the python line can stretch of multiple lines because it has brackets [ ] | |
questionlist = ["I am an amateur artist\n", | |
"I have musical talent.\n", | |
"I enjoy planning home make overs.\n", | |
"I am good at performing on stage.\n" ] | |
# now let's iterate over the questions and write questions and answers in a file until the user say stop | |
artscore = 0 | |
target = open("/home/dennis/Desktop/answers.txt",'w') | |
for actualquestion in questionlist: | |
more = input("Another question ? \n type yes and press ENTER for another question") | |
if more[0] not in ["Y","y"]: # check first char of answer | |
break # break out of the loop | |
print("\n" + actualquestion + "\n") # no quotes inseide print because it's a variable (the loop variable) | |
answer1 = ask_how_much_you_agree() | |
answer2 = ask_details() | |
target.write(actualquestion) | |
target.write(answer1) | |
target.write(answer2) | |
artscore += int(answer1) | |
# end of the iteration loop | |
target.close() # escaped the loop ( or no more questions ). | |
print ("Your art interest score of {} is most interesting.\n".format(artscore)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment