Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
"""quiz.py - simple quiz program."""
 

def main():
        """Set up the quiz."""
 
# The questions and answers are in this dictionary.
qs = {"What's the capital of England? ":"London",
"What's the third planet in the Solar System? ":"Earth",
"Who wrote \"Great Expectations\"? ": "Charles Dickens",
"Who sang \"Someone Like You\"? ":"Adele",
"Who is the current Doctor Who? ":"Peter Capaldi",
"Who is the sheriff in \"The Walking Dead\"? ": "Rick Grimes",
"Which metal is liquid at room temperature? ": "Mercury",
"Who plays Katniss in \"The Hunger Games\"? ": "Jennifer Lawrence",
"Which element combines with hydrogen to make water? ": "Oxygen",
"What is the highest mountain in the UK? ": "Ben Nevis"}
 
print("*** Quiz ***\n")
name = input("Please enter your name: ")
print()
print("\nWell done {0}, you scored {1} out of {2}).".format(name, quiz(qs), len(qs)))
 
def quiz(qs):
#Returns a score. Asks questions from qs dictionary.""
score = 0
# Use the .items() method to get the key / value pairs.
	for q,a in qs.items():
		if input(q).lower() == a.lower():
			score += 1
		print("Correct.")
	else:
		print("Sorry, correct answer is \"{}\".".format(a))
	return score
 
	if __name__ == "__main__":
		main()


What I have tried:

fixed small amount of errors like indentation blocks and that i am using python 3.4 can anyone give me hand please
Posted
Updated 22-Oct-16 6:46am
Comments
Suvendu Shekhar Giri 21-Oct-16 14:22pm    
I don't know python but is that because of you are trying access the function before it is defined, if Python works that way.
[no name] 23-Oct-16 3:48am    
Python works fine in that way sir

Line 1 is incorrect, only comments can exist at that level. Documentation comments must be inside a block.

Much of the remaining indentation is wrong; fix that and the program will run.
 
Share this answer
 
Python
# Fixed intendations

def quiz(qs):
    score = 0
    for q,a in qs.items():
        if input(q).lower()==a.lower():
            score+=1
            print('true')
    return score
 
 
def main():
    # The questions and answers are in this dictionary.
    qs = {"What's the capital of England? ":"London",
        "What's the third planet in the Solar System? ":"Earth",
        "Who wrote \"Great Expectations\"? ": "Charles Dickens",
        "Who sang \"Someone Like You\"? ":"Adele",
        "Who is the current Doctor Who? ":"Peter Capaldi",
        "Who is the sheriff in \"The Walking Dead\"? ": "Rick Grimes",
        "Which metal is liquid at room temperature? ": "Mercury",
        "Who plays Katniss in \"The Hunger Games\"? ": "Jennifer Lawrence",
        "Which element combines with hydrogen to make water? ": "Oxygen",
        "What is the highest mountain in the UK? ": "Ben Nevis"}
         
    print("*** Quiz ***\n")
    name = input("Please enter your name: ")
    print()
    print("\nWell done {0}, you scored {1} out of {2}).".format(name, quiz(qs), len(qs)))
         

 
if __name__ == "__main__":
	main()


And finally, I ran the program it works and it asked me several questions I gave the correct answers for only 4 questions london,mercury,earth and oxygen but you program said "Well done" where I actually failed in the in test you should fix it too.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900