+ 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...
6 Respuestas
+ 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")
+ 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
+ 3
Anna thank u very much....u explain very well....🤟🤟
+ 3
Thank you ☺️
+ 2
Anna i got it now thanks for ur kind help!!
+ 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"))