+ 1

i m not able to understand why the last nested if statement doesnt work if we put the value of 10...kindly check it...

https://code.sololearn.com/c780Nl7mK9BR/?ref=app

23rd Jun 2019, 9:26 AM
PRO
PRO - avatar
6 Antworten
+ 4
It's because strings, even if they contain nothing but numbers, are compared lexicographically (like words in a dictionary). Comparing numbers: 1 < 5 < 10. Comparing strings: "1" < "10" < "5". That's why if you enter e.g. "12", the condition if coding_experience > "5" will evaluate to False and it won't even check if coding_experience >= 10. You need to convert the input (string) to an integer to compare them like you would compare normal numbers: coding_experience = int(input("for what no of years ve u coded")) if coding_experience > 1: print("u seem cool as a beginner .....go ahead") if coding_experience > 5: print("thats awesome") if coding_experience >= 10: print("i gotta learn from u dude")
23rd Jun 2019, 9:31 AM
Anna
Anna - avatar
+ 3
Always read expressions that contain parentheses from the inside to the outside. input() gets input from the user as a string. To convert the input into an integer, wrap it into int(): user_input = int(input()) You could also do this: user_input = input() # gets input as a string user_input = int(user_input) # converts the string to an integer
23rd Jun 2019, 9:39 AM
Anna
Anna - avatar
+ 3
Anna thank u very much....u explain very well....🤟🤟
23rd Jun 2019, 9:41 AM
PRO
PRO - avatar
+ 3
Thank you ☺️
23rd Jun 2019, 9:42 AM
Anna
Anna - avatar
+ 2
Anna i got it now thanks for ur kind help!!
23rd Jun 2019, 9:33 AM
PRO
PRO - avatar
+ 2
Anna i had one more doubt .... why do we ve to type it coding_experience = int(input("whats ur coding experience")) and not coding_experience = input(int("whats ur coding experience"))
23rd Jun 2019, 9:35 AM
PRO
PRO - avatar