Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counter in this code
#1
Hello, I need someone to explain how the counter works in this command line.
The top of the counter = 1 but at the bottom of the command line counter = 0
a = 12
b = 5
c = 2
correct = 0
incorrect = 0
i = 0
counter = 1

print("Answer all the question.")
print("a = 12, b = 5, c = 2")
question = ["\n1. a > b","2. a % b = 0","3. 12 // 5 = c","4. a > 6 > b","5. b < 2 < a"]

for total in question:
    while counter == 1:
        print(question[i])
        i = i + 1
        if i == 5:
            answer_1 = input("Answer question 1: ")
            if answer_1 == "True" or answer_1 == "TRUE":
                correct = correct + 1
            else:
                incorrect = incorrect + 1

            answer_2 = input("Answer question 2: ")
            if answer_2 == "False" or answer_2 == "FALSE":
                correct = correct + 1
            else:
                incorrect = incorrect + 1

            answer_3 = input("Answer question 3: ")
            if answer_3 == "True" or answer_3 == "TRUE":
                correct = correct + 1
            else:
                incorrect = incorrect + 1

            answer_4 = input("Answer question 4: ")
            if answer_4 == "True" or answer_4 == "TRUE":
                correct = correct + 1
            else:
                incorrect = incorrect + 1

            answer_5 = input("Answer question 5: ")
            if answer_5 == "False" or answer_5 == "FALSE":
                correct = correct + 1
            else:
                incorrect = incorrect + 1

            counter = 0

if correct == 5:
    print("\nCongratulations, all the answers is correct.")
else:
    print("\nYou answered", correct, "questions correctly and", incorrect, "questions incorrectly.")
    print("Please do a revision")
Reply
#2
counter = 1 the variable named counter is assigned to point at an object that represents a 1.
while counter == 1: while the object assigned to counter is equal to a 1 continue to loop.
counter = 0 the variable named counter is assigned to point at an object that represents a 0.
Note: This will stop the above loop the above and only happens when if i == 5: evaluates to True.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020