i have no idea why it's not working | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

i have no idea why it's not working

input ("enter a number") int a = input if a >= 10: print('a is greater than 10') print("program ended") my_boolean print(a >= 10) i think there is a problem with variable a. what a should i do to make it work??? by the way i was learning "if" and i dont get it

6th Jun 2017, 12:00 AM
Sally Shin
Sally Shin - avatar
5 Answers
+ 19
Hi Sally! 1) You need to assign a variable to the input, so whatever the user enters will be saved in that variable and later you can call this variable to show what the user wrote. In my example I saved it in variable named "a". In your case seems like the input you expect from the user is an integer number so you have to specify it, like this: a = int(input("enter a number: ")) 2) Seems like you want to tell the user if the number he/she entered is greater than 10, so in this case the "if" statement comes in handy, this way you are telling the program to print something IF that condition is true. So, in the lines of code below you are telling the program: "If a is greater or equal than 10, print the number the user entered and tell him/her that this number is greater than 10. Also, print that the program has ended" Remember "a" is the variable that holds the input from the user, and this input was a number, so if you want to mix it with text (which is a string) you have to convert it into a string, for that we write "str(a)", like this: if a >= 10: print(str(a) + " is greater than 10") print("program ended") 3) What happens if the user enters a number that is not greater than 10? Maybe you would like to tell the user that the number he/she entered is less than 10. For that, we write an "else" statement, that comes after an "if" statement. Now the program is saying: "in case the number is NOT greater than 10, then print "a is less than 10", and then tell the user that the program has ended, like this: else: print(str(a) + " is less than 10") print("program ended") 4) Now combining the 3 steps above, we get a code that looks like this altogether: a = int(input("enter a number: ")) if a >= 10: print(str(a) + " is greater than 10") print("program ended") else: print(str(a) + " is less than 10") print("program ended")
6th Jun 2017, 3:47 AM
Pao
Pao - avatar
+ 16
Great code Taija 😊👌 In Python, we can also concatenate a string and an integer using placeholders, and substitute values into a string using the % operator. This way, any %s is replaced with a string value. Like this: print('%s' % a + ' is greater than 10') For this case, I find shorter the other way, though, I mean, using str(a)😊
6th Jun 2017, 10:38 AM
Pao
Pao - avatar
+ 7
Three things: 1) On the first line, you are asking for input but aren't assigning it to anything 2) You're using an invalid way of creating an integer, what you've written looks like Java but this is otherwise Python 3) my_boolean doesn't do anything, it's not a variable or a function Here's a working code: answer = input("enter a number") a = int(answer) if a >= 10: print('a is greater than 10') print("program ended") #my_boolean print(a >= 10) But it's still missing some things. You should definitely check whether the type of answer is an integer and catch a possible ValueError. I can show you how to do it but I'm giving you an opportunity to try first.
6th Jun 2017, 12:28 AM
Taija
Taija - avatar
+ 6
I added error handling with a try ... except statement to the code and also made the output more verbose and with more line breaks. Hope this is something along the lines of what you were looking for: https://code.sololearn.com/cRIsc049Uszn/#py If something is unclear, ask away!
6th Jun 2017, 6:56 AM
Taija
Taija - avatar
+ 1
var a=input("enter a number") use this instead of first 2 lines
6th Jun 2017, 10:31 AM
allahnoor turab
allahnoor turab - avatar