[Tutor] Help on Python

Cameron Simpson cs at cskk.id.au
Wed Oct 16 04:39:16 EDT 2019


We use the inline reply style in this list. Remarks are inline below the 
relevant parts of your email. Please do the same in your replies.

On 15Oct2019 20:20, Tyson Barber <tysonwbarber at gmail.com> wrote:
>Sorry about the confusion of my last email, this is my first time using
>this method of help.
>
>I have tweaked the code slightly to look like this:
>
>income = (input("Please enter the income: "))
>cost = int(input("Please enter the cost: "))
>if income >= cost :
>        print (income - cost == profit)
>else:
>    income < cost :
>        print (cost - income == loss)
>
>in the line after the else statement, it is giving me an error message of
>invalid syntax (where the red is).

This is a plain text mailing list. There are no colour here. I expect 
your error message concenrs this line:

    income < cost :

That is not a legal Python statement (hence the error). It should look 
like this:

    if income < cost :

However, because it is immediate in the "else" part Python has a 
convenient shorter version, where you replace this:

    else:
        if income < cost :
            print (cost - income == loss)

with this:

    elif income < cost :
            print (cost - income == loss)

The other point (already made by someone else in this list) is that:

    income < cost

is the dual of:

    income >= cost

so you don't really need the second test at all. You could just write:

if income >= cost :
        print (income - cost == profit)
else:
        print (cost - income == loss)

because in the "else" part it _must_ already be the case that income < 
cost.

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list