Python 3 Syntax Error -
method = input("is raining? ") if method=="yes" : print("you should take bus.") else: distance = input("how far in km want travel? ") if distance == > 2: print("you should walk.") elif distance == < 10 : print("you should take bus.") else: print("you should ride bike.")
nvm, fixed it..for have same problem , on grok learning indention issue , forgot write int...
so since added second question, i'll add second answer :)
in python 3, input()
function returns string, , cannot compare strings , integers without converting things first (python 2 had different semantics here).
>>> distance = input() 10 >>> distance '10' <- note quotes here >>> distance < 10 traceback (most recent call last): file "<stdin>", line 1, in <module> typeerror: unorderable types: str() < int()
to convert string integer value, use int(string)
:
>>> distance = int(distance) >>> distance 10 <- no quotes here >>> distance < 10 false
(also note code snippet above has indentation issue -- you'll end on "if distance < 2" line whether answer "yes" or not. fix this, have indent should in "else" branch in same way.)
Comments
Post a Comment