I am an APCSP student in high school and one of our open-ended assignments is to create a program that has an educational function. Here is the specific Python function I'll be calling in the main program:
def worldStudyQuestions():
worldStudying = True
while worldStudying:
print ("Thanks for studying world history!")
print ("This study guide consists of ten questions about ancient religions. \n")
w1 = "Which religion focused on the preservation of all natural things?"
w1Ans = "Jainism required adherents to move slowly so as to not kill souls living in different objects."
w2 = "Was the Roman state religion monotheistic or polytheistic?"
w2Ans = "Polytheistic; they based their gods on the Greek gods."
w3 = "What was the Buddha's original name?"
w3Ans = "Siddhartha Gautama"
w4 = "Which Greek cult often fled to the mountains to drink wine?"
w4Ans = "Cult of Dionysus"
w5 = "How many tribes of Israel existed?"
w5Ans = "Twelve"
w6 = "Did Confucians look up to a god?"
w6Ans = "No, they were primarily philosophical."
w7 = "Where did Gabriel first make contact with Muhammad?"
w7Ans = "In a cave near Mecca."
w8 = "What attracted the lower classes to Hinduism?"
w8Ans = "Hinduism disregarded the traditional caste system of imperial India."
w9 = ""
w9Ans = ""
w0 = ""
w0Ans = ""
worldQuestionList = [w1, w2, w3, w4, w5, w6, w7, w8, w9, w0]
worldAnsList = [w1Ans, w2Ans, w3Ans, w4Ans, w5Ans, w6Ans, w7Ans, w8Ans, w9Ans, w0Ans]
print ("Question 1:", random.choice(worldQuestionList))
worldStudying = False
Say random.choice selects w5 from worldQuestionList, I'd like w5Ans to be displayed when the user enters an empty string and then have w5 be removed from the randomization process on the next iteration. How do I achieve this?
Simple just pop the question and answer from the list after user answered it. Just Like
ch=random.randint(0,len(worldQuestionList))
print ("Question 1:",worldQuestionList[ch])
#Use same for answer like worldAnsList[ch]
# Finally Remove both like below
worldQuestionList.pop(ch)
worldAnsList.pop(ch)
Instead of two different lists, use a list of dictionaries.
worldQAList = [ {question: w1, answer: w1Ans}, {question: w2, answer: w2Ans}, ...]
selected = random.choice(worldQAList)
print ("Question 1:", selected['question'])
print("Answer is", selected['answer'])
While Bamer's answer is surely elegant, you may get a better understanding with this approach; it maps the questions and answers, using a dictionary of data, and a mapping of questions to answers:
import random
dataset = {
'w1': "Which religion focused on the preservation of all natural things?",
'w1Ans': "Jainism required adherents to move slowly so as to not kill souls living in different objects.",
'w2': "Was the Roman state religion monotheistic or polytheistic?",
'w2Ans': "Polytheistic; they based their gods on the Greek gods.",
'w3': "What was the Buddha's original name?",
'w3Ans': "Siddhartha Gautama",
'w4': "Which Greek cult often fled to the mountains to drink wine?",
'w4Ans': "Cult of Dionysus",
'w5': "How many tribes of Israel existed?",
'w5Ans': "Twelve",
'w6': "Did Confucians look up to a god?",
'w6Ans': "No, they were primarily philosophical.",
'w7': "Where did Gabriel first make contact with Muhammad?",
'w7Ans': "In a cave near Mecca.",
'w8': "What attracted the lower classes to Hinduism?",
'w8Ans': "Hinduism disregarded the traditional caste system of imperial India.",
'w9': "",
'w9Ans': "",
'w0': "",
'w0Ans': ""
}
worldQuestionList = ['w1', 'w2', 'w3', 'w4', 'w5', 'w6', 'w7', 'w8', 'w9', 'w0']
worldAnsList = ['w1Ans', 'w2Ans', 'w3Ans', 'w4Ans', 'w5Ans', 'w6Ans', 'w7Ans', 'w8Ans', 'w9Ans', 'w0Ans']
question_to_answer = {q: a for q, a in zip(worldQuestionList, worldAnsList)}
# this is a dictionary comprehension.
# it creates a mapping (dictionary) between questions keys and answers keys:
# {w1: 'w1Ans', 'w2': w2Ans', ...}
# this "lookup table" is used to access the questions and their corresponding answers
def worldStudyQuestions():
worldStudying = True
while worldStudying:
print ("Thanks for studying world history!")
print ("This study guide consists of ten questions about ancient religions. \n")
question = random.choice(worldQuestionList)
print("Question 1:", dataset[question])
print("Answer 1:", dataset[question_to_answer[question]])
worldStudying = False
worldStudyQuestions()
I would probably go for something like this:
def worldStudyQuestions():
worldStudying = True
qa = {'question1': 'answer1', 'question2': 'answer2', 'question3': 'answer3'}
questions = list(qa.keys())
while len(questions) > 0:
print ("Thanks for studying world history!")
choice = random.choice(questions)
print("Question: " + choice)
print("Answer: " + qa[choice])
questions.remove(choice)
What it does is basically map questions to answers in a dictionary. Then, we take a list of all questions (keys of the dictionary), and randomly pick one. Once the choice is made, we can take a corresponding answer. We then remove the choice from the questions
and continue iteration.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.