Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

practice pyhton problem


ghost's Avatar
0 0

Ok, I just started into python today. I was looking at a bunch of examples and tried a few. Then i decided to make somehting worthless just to combine some stuff. I am trying to work out using else and elif to be used together. here is what I have….

string1 = raw_input("What is your name? ")
integer1 = input("How much do you make a year? ")
string2 = raw_input ("What is your friend's name? ")
integer2 = input("How much does your friend make a year? ")
print string1, "makes", integer1, "yearly while ", string2, "makes ", integer2, "yearly ",
integer3 = integer2 - integer1
integer4 = integer1 - integer2
if integer1 < integer2:
        print string2, "makes more money than", string1, "by", integer3, "dollars",
else:
        print string1, "makes more money than", string2, "by", integer4, "dollars",
 elif integer1 == integer2
        print string1, "and", string2, "both make", integer1,

any help will be appreciated! I am here to learn


ghost's Avatar
0 0

anyone able to help me?


richohealey's Avatar
Python Ninja
0 0

in an if: elif: else:

sequence, else must be the last.

ie you have if as your first condition, then elif (else, if) as all the middle one, and then else, which is run if no prior conditions have been met.


ghost's Avatar
0 0

ok, now this is what the code looks like….

string1 = raw_input("What is your name? " )
integer1 = input("How much do you make a year? " )
string2 = raw_input ("What is your friend's name? " )
integer2 = input("How much does your friend make a year? " )
print string1, "makes", integer1, "yearly while ", string2, "makes ", integer2, "yearly ",
integer3 = integer2 - integer1
integer4 = integer1 - integer2
if integer1 < integer2:
   print string2, "makes more money than", string1, "by", integer3, "dollars",

elif integer1 > integer2:
   print string1, "makes more money than", string2, "by", integer4, "dollars",

else:
  print string1, "and", string2, "both make", integer1, “dollars”,

now when i run it I get a syntax error at the if and I dunno why.


ghost's Avatar
0 0

try indenting?

<tab here>do whatever```

I know a little python and you have to indent wtih if statements

Edit: nvm, here:

```markupprint "Money Maker!"
string1 = raw_input("What is your name? " )
integer1 = input("How much do you make a year? " )
string2 = raw_input ("What is your friend's name? " )
integer2 = input("How much does your friend make a year? " )
print string1, "makes", integer1, "yearly while ", string2, "makes ", integer2, "yearly ",
integer3 = integer2 - integer1
integer4 = integer1 - integer2
if integer1 < integer2:
	print string2, "makes more money than", string1, "by", integer3, "dollars",

elif integer1 > integer2:
	print string1, "makes more money than", string2, "by", integer4, "dollars",

else:
	print string1, "and", string2, "both make", integer1, "dollars"```

that works, you had wrong quotes on last line