This is a section of a program I made that keeps track of some money I'm saving. :cool:
When I enter how much I want to add it saves what I enter to a file and updates the current amount I have saved, also in the file.

When I enter 0 I want the program to go back to the main function which asks what I want to do, (Code below else: ) but it does not work. It still writes that I entered 0 into the file. It just seems to skip the code that checks if amount is 0.

I don't see why this doesn't work. I also tried putting it right below 'amount' and the program still skipped it.

def add():
    while True:
        try:
            amount = float(raw_input("How much do you want to add? Enter 0 to cancel.\n"))
                
        except ValueError:
            print "Invalid input."
            os.system("pause")

        else:
                if amount == "0":
                print "test: float is 0"
                os.system("pause")
                os.system("cls")
                main()

            ####Write to file and return to main####

Because you are checking for a string, when you changed the variable "amount" to a float.

change line 11 to

if amount == 0:

or this

if not amount:

Thank you! That worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.