I am in a Fundamentals of Programming class which we are using Python. In the book was a simple code to solve a problem where a cook knows in cups how much he needs but we are converting it to ounces. I was playing around with that and trying to figure out the if-else, elif commands. It is telling me where it says

if choice ==1:

that there is a syntax error.

Can someone let me know what I am doing wrong?

def main():
    intro()
    c2o = 1
    o2c = 2
    choice = int(input('Enter 1 if you would like to convert cups to ounces', \
                       'or enter 2 if you would like to convert ounces to cups.')
    if choice == 1:
        cups_needed = int(input('Enter the number of cups: '))
        cups_to_ounces(cups_needed)
    elif choice == 2:
        ounces_needed = int(input('Enter the number of ounces: '))
        ounces_to_cups(ounces_needed)
    print()


def intro():
    print('This program converts measurements')
    print('in cups to fluid ounces or fluid ounces to cups. For your')
    print('reference the formula is:')
    print(' 1 cup = 8 fluid ounces or 8 fluid ounces = 1 cup')
    print()

def cups_to_ounces(cups):
    ounces = cups * 8
    print('That converts to', ounces, 'ounces.')

def ounces_to_cups(ounces):
    cups = ounces / 8
    print('That converts to', cups, ' cups.')

main()

You are using Python 3 aren't you? The code looks OK for me except I would return value from function, not print from there.

Could you give the exact error message and your input.

I noticed a few things.

First, there's no reason to put the entire code into a function (eg main). This is fine, but not mandatory. There's a special line at the bottom of your code that you should add so that if this program is called as an import from another program, then main() won't be executed. It will only be executed if you run the program (ie python thisprogram.py).

Change main() to:

if __name__=='__main__':
    main()

Note that the use of main() and main is just a coincidence. If your main() function was called "main_insane" then it would still be:

if __name__=='__main__':
    main_insane()

Second, why do you have these variables otc and cto?

Third, I would add an error if the use doesn't enter 1 or 2. Something like:

if choice == 1:
    ....
elif choice == 2:
   ...
else:
   raise InputError('Please enter 1 or 2!')

This will let the user know they screwed up.

I actually added that while I was waiting for responses. I have the c2o or o2c as choices. cups2ounces or ounces2cups. Is that not needed? I am just new :\

Also you are saying to start with a "if" statement?

to cups.')) is missing line 5.

Perfect snippsat. That was it.

I actually added that while I was waiting for responses. I have the c2o or o2c as choices. cups2ounces or ounces2cups. Is that not needed? I am just new :\

Also you are saying to start with a "if" statement?

I'm saying you define two variables, c2o and o2c and then never use them.

Yes. Your code would literally only change at the bottom:

main()

becomes:

if __name__=='__main__':
    main()

The code should run fine and you won't notice any differences.

you need to complete line 6 by adding a ) at the end

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.