I'm a Python beginner, I took a month break and today I'm back. In the book that I'm reading to learn Python the author asks us to memorize truth tables. I wanted to make it more fun, so I came up with the idea to try to write a script that asks me random questions (I wrote) from a list, which I can answer to with "true" or "false". The problem is that a lot of the things I used in the code I haven't done from the book yet, so I used the internet to help me.
This is what I came up with:
print("Here are some questions for you, answer true or false.")
input("Press 'Enter' if you would like to continue.")
def a():
answer = input("1) not False: ")
print("Correct!") if answer == 'true' else print("Wrong!")
def b():
answer = input("2) not True: ")
print("Correct!") if answer == "false" else print ("Wrong!")
def c():
answer = input("3) True or False: ")
print("Correct!") if answer == "true" else print ("Wrong!")
import random
questions = [a(), b(), c()]
print(random.sample(questions, 3))
The questions are a lot more, I've used only three just to test if the code was working. So far so good, if it weren't for the fact that I can't seem to call random functions from my list.
This is my output: output
Also, what does [None, None, None] mean?
I know it's not a good idea to use things that I haven't even studied yet, but I was very excited about this.
TIA!
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.
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()
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)]
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")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With