+ 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?
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)
+ 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.
+ 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
+ 1
Thank you
+ 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.
+ 1
So i need to mention int if i want to input a integer instead of string?
+ 1
Thank you