1
  method = input("Is it currently raining? ")
if method=="Yes" :
  print("You should take the bus.")
else: distance = input("How far in km do you want to travel? ")
if distance == > 2:
    print("You should walk.")
elif distance ==  < 10 :
  print("You should take the bus.")
else: 
  print("You should ride your bike.")

Nvm, i fixed it..for those who have the same problem and were on Grok Learning it was just an indention issue and I forgot to write int...

5
  • Please don't change the code in your question after you've received answers. It renders the answers meaningless. Commented Jul 21, 2013 at 23:12
  • @RichieHindle sorry I didn't know...I just needed to double check i copied the right code... :0 sorry Commented Jul 21, 2013 at 23:15
  • I've rolled this back to the code you have received answers for. Commented Jul 22, 2013 at 18:02
  • I answered his second question below (before you did that), so now you've rendered my answer meaningless :) Commented Jul 23, 2013 at 9:44
  • For the distance error, see the "also note that your code snippet" part of my second reply -- the "if distance" part will run for both branches of the first if statement, since it's not indented, but distance will only be assigned in the else branch. You have to clean up the indentation so that things that belong together start in the same column. Commented Jul 25, 2013 at 10:08

2 Answers 2

2

So since you added a second question, I'll add a second answer :)

In Python 3, the input() function always returns a string, and you cannot compare strings and integers without converting things first (Python 2 had different semantics here).

>>> distance = input()
10
>>> distance
'10' <- note the quotes here
>>> distance < 10
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() < int()

To convert a string to an integer value, use int(string):

>>> distance = int(distance)
>>> distance
10 <- no quotes here
>>> distance < 10
False

(also note that your code snippet above has an indentation issue -- you'll end up on the "if distance < 2" line whether you answer "Yes" or not. To fix this, you have to indent everything that should be in the "else" branch in the same way.)

Sign up to request clarification or add additional context in comments.

Comments

2

You need to specify what to compare with for every comparison, so

elif distance <=2 and >=10 

should be:

elif distance <=2 and distance >=10:

(there are more clever ways to do this, but the above is the quickest fix)

7 Comments

It's odd though 'cause logically that makes sense but it still shows an invalid syntax error! (Could it be due to wrong indention?)
@fredrik I have tried what you said and fixed the indention but it still shows wrong syntax (pointing at the 10 this time)...What have i done wrong? :/
@icktoofay What is COBOL??
@Joshua: A rather old programming language that is very likely irrelevant to you. Perhaps I shouldn't have mentioned it.
@icktoofay Oh ok if its anything above high school knowledge it's probably irrelevant :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.