tags:

views:

180

answers:

3

I'm very new so just learning, so go easy please!

start = int(input('How much did you start with?:' ))
if start < 0:
    print("That's impossible!  Try again.")
    print(start = int(input('How much did you start with:' )))
if start >= 0:
    print(inorout = raw_input('Cool!  Now have you put money in or taken it out?: '))
    if inorout == in:
        print(in = int(raw_input('Well done!  How much did you put in?:')))
        print(int(start + in))

This always results in syntax error? I'm sure I'm doing something obvious wrong!

Thanks!

+7  A: 
  • You can't assign to variables in expressions in Python, like in C: print (start=int(input('blah'))) isn't correct. Do the assignment first in a separate statement.
  • The first line musn't be indented, but that might just be a copy and paste error.
  • The word in is a reserved word so you can't use it for variable names
Doug
And `in` isn't defined in the if-condition and there is either input (python 3) or raw_input.
Dario
should have been inorout == "in", reserved word not intended as variable name.
gimel
+3  A: 

Assigning in statements is your problem. Move the assignments out of print statements

KIV
print is a function here.
SilentGhost
A: 
  • Consider prompting for input using a function wrapping a loop.
  • Don't use input for general user input, use raw_input instead
  • Wrap your script execution in a main function so it doesn't execute on import


def ask_positive_integer(prompt, warning="Enter a positive integer, please!"):
    while True:
        response = raw_input(prompt)
        try:
            response = int(response)
            if response < 0:
                print(warning)
            else:
                return response
        except ValueError:
            print(warning)

def ask_in_or_out(prompt, warning="In or out, please!"):
    '''
    returns True if 'in' False if 'out'
    '''
    while True:
        response = raw_input(prompt)
        if response.lower() in ('i', 'in'): return True
        if response.lower() in ('o', 'ou', 'out'): return False
        print warning

def main():
    start = ask_positive_integer('How much did you start with?: ')
    in_ = ask_in_or_out('Cool!  Now have you put money in or taken it out?: ')
    if in_:
        in_amount = ask_positive_integer('Well done!  How much did you put in?: ')
        print(start + in_amount)
    else:
        out_amount = ask_positive_integer('Well done!  How much did you take out?: ')
        print(start - out_amount)

if __name__ == '__main__':
    main()
jsamsa