0

This is some of my homework but i am really stuck and can't find any advice online.

def main():
        endProgram = 'n'
        print()
        while endProgram == 'n':
            total = 0
            totalPlastic = 0
            totalMetal= 0
            totalGlass = 0
            endCount = 'n'
            while endCount == 'n':
                    print()
                    print('Enter 1 for Plastic')
                    print('Enter 2 for Metal')
                    print('Enter 3 for Glass')
                    option = int(input('Enter now: '))
                    if option == 1:
                            totalPlastic = getPlastic(totalPlastic)
                    elif option == 2:
                            totalMetal = getMetal(totalMetal)
                    elif option == 3:
                            totalGlass = getGlass(totalGlass)
                    else:
                            print('You have entered an invalid input')
                            return main()

                    endCount = input('Do you want to calculate the total? (y/n): ')
            print()
            total = calcTotal(totalPlastic, totalMetal, totalGlass)
            printNum(total)
            break

            endProgram = input('Do you want to end the program? (y/n): ')


def getPlastic(totalPlastic):
        plasticCount = int(input('Enter the number of Plastic bottles you have: '))
        totalPlastic = totalPlastic + plasticCount * .03
        return totalPlastic

def getMetal(totalMetal):
        metalCount = int(input('Enter the number of Metal cans you have: '))
        totalMetal = totalMetal + metalCount * .05
        return totalMetal

def getGlass(totalGlass):
        glassCount = int(input('Enter the number of Glass bottles you have: '))
        totalGlass = (totalGlass + glassCount * .10)
        return totalGlass

def calcTotal(totalPlastic, totalMetal, totalGlass):
        total = totalPlastic + totalMetal + totalGlass
        return total

def printNum(total):
    print('Your total recyclable value is $', total)

    main()

My problem is I run the code and it works fine for 99% of it. The program will ask any type and amount of bottle, and it totals it correctly too, the issue is that the outer loop never gets asked. After it prints the total it just goes right back to asking what type of bottle you have instead of asking you whether or not you want to end the program. Any help is appreciated thanks.

3 Answers3

0

Remove the break just before Do you want to end the program? (y/n)

Aven Desta
  • 2,114
  • 12
  • 27
0
print()
total = calcTotal(totalPlastic, totalMetal, totalGlass)
printNum(total)
break

Here you are using break and the loop gets out and does not go forward. break is used in while loop endProgram == 'n' and it breaks and never goes forwards.

One more thing it is a bad habbit of making defining first. Always define main after the other functions and do not just call main().

Use this -

if(__name__ == '__main__'):
       main()
  • Thank you for your help this solved my problem. The break was left in there from a previously failed attempt to make the program function as intended. Code blindness is real. Thanks. – Joe Kruse Feb 25 '20 at 13:47
0

The break statement was exiting your script, therefore your line endProgram = input('Do you want to end the program? (y/n): ') was unreachable, that is why it was never "asked".

Also, fixed some of your indentation, give it a go.

def main():
    endProgram = 'n'
    print()
    while endProgram == 'n':
        total = 0
        totalPlastic = 0
        totalMetal = 0
        totalGlass = 0
        endCount = 'n'
        while endCount == 'n':
            print()
            print('Enter 1 for Plastic')
            print('Enter 2 for Metal')
            print('Enter 3 for Glass')
            option = int(input('Enter now: '))
            if option == 1:
                totalPlastic = getPlastic(totalPlastic)
            elif option == 2:
                totalMetal = getMetal(totalMetal)
            elif option == 3:
                totalGlass = getGlass(totalGlass)
            else:
                print('You have entered an invalid input')
                return main()

            endCount = input('Do you want to calculate the total? (y/n): ')
        print()
        total = calcTotal(totalPlastic, totalMetal, totalGlass)
        printNum(total)


        endProgram = input('Do you want to end the program? (y/n): ')


def getPlastic(totalPlastic):
    plasticCount = int(input('Enter the number of Plastic bottles you have: '))
    totalPlastic = totalPlastic + plasticCount * .03
    return totalPlastic


def getMetal(totalMetal):
    metalCount = int(input('Enter the number of Metal cans you have: '))
    totalMetal = totalMetal + metalCount * .05
    return totalMetal


def getGlass(totalGlass):
    glassCount = int(input('Enter the number of Glass bottles you have: '))
    totalGlass = (totalGlass + glassCount * .10)
    return totalGlass


def calcTotal(totalPlastic, totalMetal, totalGlass):
    total = totalPlastic + totalMetal + totalGlass
    return total


def printNum(total):
    print('Your total recyclable value is $', total)

main()
gmwill934
  • 609
  • 1
  • 10
  • 27