Home > Net >  Why does my tip calculator code print the %'s twice?
Why does my tip calculator code print the %'s twice?

Time:09-30

The calculator works for what I want it to do, which is print 3 different percentages for the one input. I just don't know why it prints the tip amounts twice for every answer.

print("Tip Calculator")

print()

re_run = "y"

while re_run.lower() == "y":
    cost_of_meal = float(input("Cost of meal: "))

    tip_percent_1 = (15)                                   # Tip Percents

    tip_percent_2 = (20)

    tip_percent_3 = (25)


    tip_1 = cost_of_meal * (tip_percent_1/100)

    tip_1 = round(tip_1, 2)

    total_amount_1 = round(cost_of_meal   tip_1, 2)


    tip_2 = cost_of_meal * (tip_percent_2/100)

    tip_2 = round(tip_2, 2)

    total_amount_2 = round(cost_of_meal   tip_2, 2)


    tip_3 = cost_of_meal * (tip_percent_3/100)

    tip_3 = round(tip_3, 2)

    total_amount_3 = round(cost_of_meal   tip_3, 2)


    print()

    for tip_percent_1 in str(15):
        print("15%")
        print(("Tip amount: ")   str(tip_1))
        print(("Total amount: ")   str(total_amount_1)   "\n")

    for tip_percent_2 in str(20):
        print("20%")
        print(("Tip amount: ")   str(tip_2))
        print(("Total amount: ")   str(total_amount_2)   "\n")

    for tip_percent_3 in str(25):
        print("25%")
        print(("Tip amount: ")   str(tip_3))
        print(("Total amount: ")   str(total_amount_3)  "\n")

    print()


    re_run = input("\nContinue? y/n:")

    if re_run.lower() != "y":

        break


print("\nThank you for using the tip calculator, bye!")

CodePudding user response:

It's because you're for-looping on a string, which makes you enter the for-loop once for every character

for example:

for i in str(25):
    print(i)

>>> '2'
>>> '5'

To fix this substitute the str() with the list/tuple of str()s or int()s, or just remove it really since you're not looping.

print("Tip Calculator")

print()

re_run = "y"

while re_run.lower() == "y":

    cost_of_meal = float(input("Cost of meal: "))

    tip_percent_1 = (15)                                   # Tip Percents

    tip_percent_2 = (20)

    tip_percent_3 = (25)


    tip_1 = cost_of_meal * (tip_percent_1/100)

    tip_1 = round(tip_1, 2)

    total_amount_1 = round(cost_of_meal   tip_1, 2)


    tip_2 = cost_of_meal * (tip_percent_2/100)

    tip_2 = round(tip_2, 2)

    total_amount_2 = round(cost_of_meal   tip_2, 2)


    tip_3 = cost_of_meal * (tip_percent_3/100)

    tip_3 = round(tip_3, 2)

    total_amount_3 = round(cost_of_meal   tip_3, 2)


    print()

    for tip_percent_1 in (15,):
        print("15%")
        print(("Tip amount: ")   str(tip_1))
        print(("Total amount: ")   str(total_amount_1)   "\n")

    for tip_percent_2 in (20,):
        print("20%")
        print(("Tip amount: ")   str(tip_2))
        print(("Total amount: ")   str(total_amount_2)   "\n")

    for tip_percent_3 in (25,):
        print("25%")
        print(("Tip amount: ")   str(tip_3))
        print(("Total amount: ")   str(total_amount_3)  "\n")

    print()


    re_run = input("\nContinue? y/n:")

    if re_run.lower() != "y":

        break


print("\nThank you for using the tip calculator, bye!")

I optimized the code so it's more simple and dynamic:

print("Tip Calculator")

print()

re_run = "y"

while re_run.lower() == "y":

    cost_of_meal = float(input("Cost of meal: "))

    tip_percents = (15, 20, 25)

    print()

    for tip_percent in tip_percents:
        
        tip = cost_of_meal * (tip_percent/100)
        tip = round(tip, 2)
        total_amount = round(cost_of_meal   tip, 2)

        print(str(tip_percent) "%")
        print(("Tip amount: ")   str(tip))
        print(("Total amount: ")   str(total_amount)   "\n")

    print()


    re_run = input("\nContinue? y/n:")

    if re_run.lower() != "y":

        break


print("\nThank you for using the tip calculator, bye!")

CodePudding user response:

It is because you print them in for loops. if you remove "for tip_percent_1 in str(15):" (and others) it will only print once.

CodePudding user response:

You are printing under the loops - for tip_percent_1 in str(15):, this will run two times and print two times. So, I suggest you remove it -

print("15%")
print(("Tip amount: ")   str(tip_1))
print(("Total amount: ")   str(total_amount_1)   "\n")

print("20%")
print(("Tip amount: ")   str(tip_2))
print(("Total amount: ")   str(total_amount_2)   "\n")

print("25%")
print(("Tip amount: ")   str(tip_3))
print(("Total amount: ")   str(total_amount_3)  "\n")

CodePudding user response:

You don't want the for loops. For example, change

for tip_percent_2 in str(20):
    print("20%")
    print(("Tip amount: ")   str(tip_2))
    print(("Total amount: ")   str(total_amount_2)   "\n")

to just

print("20%")
print(("Tip amount: ")   str(tip_2))
print(("Total amount: ")   str(total_amount_2)   "\n")

This is because str(20) gives "20" and when you iterate over a string, you iterate over the characters in the string, so your for-loop is iterating over the elements in the list ['2', '0']; since the list has two elements, your print statements execute twice.

  • Related