The problem
Using parens calls the function. What you need to do is store references to the functions: questions = [a, b, c]
.
Since None
is returned by your functions implicitly (due to lack of explicit return; see this post), and because you merely called the functions when creating the questions
list, all you did was answer each question (returning None
), and shuffle the answers around.
Solution 1
By storing references to your functions in a questions
list, this allows you to simply call the functions in a for
loop iterating over the shuffled list:
questions = [a, b, c]
for q in random.sample(questions, 3):
q()
Solution 2
You can modify your function to return points, which would allow you to start tracking your performance:
# Import statements should always be placed at the top of the file
import random
print("Here are some questions for you, answer true or false.")
input("Press 'Enter' if you would like to continue.") # this does nothing
def a():
answer = input("1) not False: ")
print("Correct!") if answer == "true" else print("Wrong!")
return int(answer.lower() == "true")
def b():
answer = input("2) not True: ")
print("Correct!") if answer == "false" else print ("Wrong!")
return int(answer.lower() == "false")
def c():
answer = input("3) True or False: ")
print("Correct!") if answer == "true" else print ("Wrong!")
return int(answer.lower() == "true")
questions = [a, b, c]
earned_points = [q() for q in random.sample(questions, 3)]
Solution 3
Even better, though, would be to use the if __name__ == "__main__"
pattern:
import random
def a():
answer = input("1) not False: ").lower()
print("Correct!") if answer == "true" else print("Wrong!")
return int(answer == "true")
def b():
answer = input("2) not True: ").lower()
print("Correct!") if answer == "false" else print ("Wrong!")
return int(answer == "false")
def c():
answer = input("3) True or False: ").lower()
print("Correct!") if answer == "true" else print ("Wrong!")
return int(answer == "true")
if __name__ == "__main__":
print("Here are some questions for you, answer true or false.")
proceed = input("Press 'Enter' if you would like to continue.")
if proceed == '':
questions = [a, b, c]
earned_points = [q() for q in random.sample(questions, 3)]
# now do something with earned_points, for example:
# calculate and print the total points scored
# calculate and print the user's 'grade' (their score out of the total)
# calculate and print the average score
else:
print("You declined to play")
Further Improvements
There are numerous other improvements you can make to your program as well.
Note that the user's input is immediately converted to lowercase in the third solution -- this simplifies input validation. You could even write a helper function which complains if the user enters anything but 'true' or 'false'.
Something else thing to notice is that the code in your three functions is identical save for the question prompt and the answer. One thing you could do is implement a generic "question asker" function which takes two arguments: a prompt, and the expected answer. Then you'd store your questions as 2-tuples like ("true or false", "true")
, or ("true and false", "false")
, etc. Then you'd shuffle the question tuples then iterate over the shuffled list, passing them to the generic question asker.