+ 1

How can i use the value of input ?

I am 14 . I want to Print True When I input 14 else i want To print false . Can Anyone help me with this code?

5th Jun 2017, 8:11 AM
Tarif Aziz
Tarif Aziz - avatar
7 Réponses
+ 2
psudo code : var i <- user_input if ( i == 14 ) print "true" else print "false" ===better way if your language supports=== var i <- user_input print (i == 14)
5th Jun 2017, 8:15 AM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 2
Yes that's the idea but be aware in programmation we haven't to forget that the user could to anything and we have to handle all the possibilities. Nothing it worse than a program that fails.
5th Jun 2017, 8:29 AM
Glozi30
Glozi30 - avatar
+ 1
i assume you are coding with python this code should work : age = int(input("How old are you ?")) if age == 14: # == to check an equality print(True) else: print(False) you could also do : print(age == 14) Or print(True if age==14 else False) #ternary operator
5th Jun 2017, 8:16 AM
Glozi30
Glozi30 - avatar
+ 1
Thank you
5th Jun 2017, 8:17 AM
Tarif Aziz
Tarif Aziz - avatar
+ 1
In reality the input() function in python always returns a string that's why sometimes you have to cast it to int or float : for example age = int(input("How old are you ? ")) But be aware they may be errors in your code if the user writes "Bananas" for example python won't be able to cast this string to a int so it will fail. The solution is to handle exceptions with try and except blocks.
5th Jun 2017, 8:25 AM
Glozi30
Glozi30 - avatar
+ 1
So i need to mention int if i want to input a integer instead of string?
5th Jun 2017, 8:27 AM
Tarif Aziz
Tarif Aziz - avatar
+ 1
Thank you
5th Jun 2017, 8:33 AM
Tarif Aziz
Tarif Aziz - avatar