https://clonerobbylab.w0w.fr

Workshop WS 4 : create a function/ create a Quizz

def check_odd_even():
num = int(input(“Enter a number: “))
if num % 2 == 0:
print(“The number is Even.”)
else:
print(“The number is Odd.”)

#check_odd_even()


#create a function check_odd_even

#improve the function like check_odd_even(number)
#try it
def check_odd_even(number):
if number % 2 == 0:
print(“The number is Even.”)
else:
print(“The number is Odd.”)

check_odd_even(8)

 

Programming a Quizz !!

def run_quiz():
questions = [
{
“question”: “What is the capital of France?”,
“options”: [“A) Berlin”, “B) Madrid”, “C) Paris”, “D) Rome”],
“answer”: “C”
},
{
“question”: “Which language is primarily used for web development?”,
“options”: [“A) Python”, “B) JavaScript”, “C) C++”, “D) Java”],
“answer”: “B”
},
{
“question”: “What is 5 + 7?”,
“options”: [“A) 10”, “B) 11”, “C) 12”, “D) 13”],
“answer”: “C”
},
{
“question”: “Who developed the theory of relativity?”,
“options”: [“A) Isaac Newton”, “B) Nikola Tesla”, “C) Albert Einstein”, “D) Galileo Galilei”],
“answer”: “C”
}
]

score = 0

for q in questions:
print(“\n” + q[“question”])
for option in q[“options”]:
print(option)

answer = input(“Enter your choice (A/B/C/D): “).strip().upper()
if answer == q[“answer”]:
print(“Correct!”)
score += 1
else:
print(f”Wrong! The correct answer was {q[‘answer’]}”)

print(f”\nQuiz finished! Your score: {score}/{len(questions)}”)

if __name__ == “__main__”:
run_quiz()