Home > Enterprise >  How do I count the number of times an else statement is performed and executed in my program
How do I count the number of times an else statement is performed and executed in my program

Time:09-14

This program is supposed to do basic calculations by asking the user what type of calculation it wants to perform, then it collects two numbers via the user input and finally performs the calculation by spitting it out before asking you to close it. In the else statement I have here, if the user doesn't input what I want them to, it'll tell them to read more carefully and then it restarts the program. What I want to do is make it so that if the else statement has been executed 3 or 4 times, it will print "I give up" and then it asks the user to type anything to exit the program. I'm a beginner too!

while True:

    Pick = input("add, sub, multiply, or divide? :")


    # Pick the calculation and then actually do it
    if Pick == "add":
        num_one = float(input("First number: "))
        num_two = float(input("Second number:"))
        if num_one == 9 and num_two == 10:
            print("Twenty one? You stupid.")
            Exit = input("Type literally anything to exit ")
            break
        print("The sum is : ", num_one   num_two)
        Exit = input("Type literally anything to exit ")
        break
    elif Pick == "sub":
        num_one = float(input("First number: "))
        num_two = float(input("Second number:"))
        print("The result is : ", num_one - num_two)
        Exit = input("Type literally anything to exit ")
        break
    elif Pick == "multiply":
        num_one = float(input("First number: "))
        num_two = float(input("Second number:"))
        print("The product is : ", num_one * num_two)
        Exit = input("Type literally anything to exit ")
        break
    elif Pick == "divide":
        num_one = float(input("First number: "))
        num_two = float(input("Second number:"))
        print("The quoitient is : ", num_one / num_two)
        Exit = input("Type literally anything to exit ")
        break
    else:
        print("Read and type exactly what it says in the beginning.")
        continue

CodePudding user response:

You can simply count how often your loop runs, so before the the loop you would have to add a loop counter, which you increase whenever the last else is called. Within the last else you then simply have to check how high the counter is. See for example:

loop_counter = 0
while True:
...
  else:
    if loop_counter >= 4:
        print("Okay, I give up!")
        Exit = input("Type literally anything to exit ")
        break
    else:
        loop_counter = loop_counter  1
        print("Read and type exactly what it says in the beginning.")
        continue

CodePudding user response:

Hope this helps you

i = 0
while True:


Pick = input("add, sub, multiply, or divide? :")

print("NUMBER OF ELSE : " str(i))
# Pick the calculation and then actually do it
if Pick == "add":
    num_one = float(input("First number: "))
    num_two = float(input("Second number:"))
    if num_one == 9 and num_two == 10:
        print("Twenty one? You stupid.")
        Exit = input("Type literally anything to exit ")
        break
    print("The sum is : ", num_one   num_two)
    Exit = input("Type literally anything to exit ")
    break
elif Pick == "sub":
    num_one = float(input("First number: "))
    num_two = float(input("Second number:"))
    print("The result is : ", num_one - num_two)
    Exit = input("Type literally anything to exit ")
    break
elif Pick == "multiply":
    num_one = float(input("First number: "))
    num_two = float(input("Second number:"))
    print("The product is : ", num_one * num_two)
    Exit = input("Type literally anything to exit ")
    break
elif Pick == "divide":
    num_one = float(input("First number: "))
    num_two = float(input("Second number:"))
    print("The quoitient is : ", num_one / num_two)
    Exit = input("Type literally anything to exit ")
    break
else:
    i = i   1
    print("Read and type exactly what it says in the beginning.")
    continue

The i variable will count the number of else executed

The rest is your job to do :))

CodePudding user response:

I have removed some code duplication in your code, let me know if you have any questions about what I have done :)

while True:
    user_input = input("add, sub, multiply, or divide? :")

    num_one = float(input("First number: "))
    num_two = float(input("Second number:"))

    # Pick the calculation and then actually do it
    if user_input == "add":
        if num_one == 9 and num_two == 10:
            print("Twenty one? You stupid.")
            break
        print(f"The sum is : {num_one   num_two}")
        break
    elif user_input == "sub":
        print(f"The result is : {num_one - num_two}")
        break
    elif user_input == "multiply":
        print(f"The product is : {num_one * num_two}")
        break
    elif user_input == "divide":
        print(f"The quoitient is : {num_one / num_two}")
        break
    else:
        print("Read and type exactly what it says in the beginning.")
        continue
Exit = input("Type literally anything to exit ")

As for the counting, you can set a counter variable before the if statement to 0, and in each elif/else statement, it can be incremented by 1.

So it would look like this:

counter = 0

# in elif/else
counter  = 1
  • Related