samedi 7 mars 2020

I would like to create a quiz game using python that picks random question from list without repetition

This code will let 2 players answers a random question from a list then compare their scores
Problems:
1. Questions keep repeating because of randint, tried using random shuffle no luck
2. If I input an answer that is on the answer list on different questions it still printing "correct"
maybe because of this code "if guess.lower() == answers[a].lower():"
3. I can't get to make it work that if an answer is wrong it will print "wrong"
I tried adding this code and it works but If I input the correct answer it still returning "wrong"

elif guess.lower() != answers[a].lower():
                print("Wrong!")
                break  

My Code

from random import randint
print("====== Quiz  Game ======")
print("")
questions = [
             "What star is in the center of the solar system?:",
             "What is the 3rd planet of the solar system?:",
             "What can be broken, but is never held?:",
             "What part of the body you use to smell?:",
             "How many days in one year?:",
             "How many letters in the alphabet?:",
             "Rival of Intel?:",
             "No.8 element on periodic element?:",
             "What is the galaxy that contains our solar system?:",
             "What animal is the king of the jungle?:"
             ]
answers = [
           "Sun",
           "Earth",
           "Promise",
           "Nose",
           "365",
           "26",
           "AMD",
           "Oxygen",
           "Milky Way",
           "Lion"
           ]

p1score = 0
p2score = 0
def playerOne():
    global p1score
    for i in range(5):
        q = randint(0,9)
        guess = input(questions[q])
        for a in range(len(answers)):
            if guess.lower() == answers[a].lower():
                print("Correct!")
                p1score += 1
                break
            else:
                continue
    print("Your score is:",p1score)
    print("")

def playerTwo():
    global p2score
    for i in range(5):
        q = randint(0,9)
        guess = input(questions[q])
        for a in range(len(answers)):
            if guess.lower() == answers[a].lower():
                print("Correct!")
                p2score += 1
                break
            else:
                continue
    print("Your score is:",p2score)
    print("")

def quiz():
    global p1score
    global p2score
    print("======Student no.1======")
    playerOne()
    print("======Student no.2======")
    playerTwo()
    if p1score > p2score:
        print("Student 1 has the highest score!")
    elif p1score < p2score:
        print("Student 2 has the highest score!")
    elif p1score == p2score:
        print("Students are tied!")
    else:
        print("Invalid")

quiz()

Image of Problem no.1 and 2

Image of Problem no.3

I need your help




Aucun commentaire:

Enregistrer un commentaire