mardi 8 novembre 2016

Nest IF needed? in Python

This is my first Python program where I've used if, while and functions. I've also passed parameters. The problem is the IF. Can you help me? I wanted the program to give the user two tries to answer and then end. If correct then it ends but if not correct it doesn't stop, keeps looping.

"""this is a quiz on computer science"""

q1Answer="c"

def questionOne():
    print("Here is a quiz to test your knowledge of computer science...")
    print()
    print("Question 1")
    print("What type of algorithm is insertion?")
    print()
    print("a....searching algorithm")
    print("b....decomposition ")
    print("c....sorting algorithm ")
    print()


def checkAnswer1(q1Answer): #q1Answer is a global variable and is needed for this function so it goes here as a parameter
    attempt=0       #These are local variables
    score=0
    answer = input("Make your choice >>>>  ")
    while attempt <1:
        if answer==q1Answer:
            attempt= attempt+1
            print("Correct!")
            score =score + 2
            break

        elif answer != q1Answer:
            answer =input("Incorrect response – 1 attempt remaining, please try again: ")
            if answer ==q1Answer:
                attempt = attempt + 1
                print("Correct! On the second attempt")
                score =score + 1
                break
        else:
            print("That is not correct\nThe answer is "+q1Answer )
            score =0

    return score    # This is returned so that it can be used in other parts of the program


##def questionTwo():
##    print("Question 2\nWhat is abstraction\n\na....looking for problems\nb....removing irrelevant data\nc....solving the problem\n")

def main():
    q1answer = questionOne()

    score = checkAnswer1(q1Answer)
    print ("Your final score is ", score)

main()

Aucun commentaire:

Enregistrer un commentaire