How to get an string character from user and print it in output | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to get an string character from user and print it in output

17th Jan 2017, 2:52 PM
Akshay
Akshay - avatar
4 Answers
+ 2
you can take strings from a user via the input function like this: input() variables allow you to temporarily store data for use within the program, you tell python you are making a variable and give it a value by typing a name for the variable (that isn't any other function name) and the = symbol then whatever you want to store in the variable to the right of the = So to make a variable called myVariableName and store a users input you can type: myVariableName = input() I should say at this point that code playground runs python code in non-interactive mode. Non-interactive mode runs code that contains the input function in a bit of a weird way - it requests all the inputs the moment you click run and then starts running the program from the top, rather than run the code up to the input and then ask for input and then continue. This caused me some confusion when I started with python, and seems to confuse lots of other people on here. to print the contents of a variable you can just put print(variableName), so the full way to take a string from user and then print it, using a variable called myVariableName is: myVariableName = input() print(myVariableName) You can name the variable whatever you like, within certain rules (such as it can't clash with function names because then python would confuse them). You could also avoid using a variable, but usually people want to also do something with the user input as well. print(input())
17th Jan 2017, 3:25 PM
Phil
Phil - avatar
+ 2
The error message you are getting is for when python tries to use what it thinks is supposed to be a variable, but it can't find a definition of it (the bit with var = whatever), in this case it's saying there's the word akshay in your code. So for it to say that error message, you've probably typed akshay directly into the code - so it's reading: var=input () print (var) akshay or something like that and when it gets to the akshay bit python gets confused and basically just throws it's arms up and says "I don't know what that akshay thing is" and stops. I'm guessing you are new to this app, and are having the same problem with code playground being awkward with inputs and mistaking it for being a problem with your code. The code var=input () print (var) looks perfect to me so I put it into code playground, to see if I get an error. When I hit run it starts in code playgrounds usual way for code that has an input() - a pop up appeared asking for input, so I put in akshay - and it ran without error and showed the string akshay in the output box as I'd expect. So this confirms to me that you should be fine, if you just put your input into the pop-up that happens when you hit run and not with the code
17th Jan 2017, 4:34 PM
Phil
Phil - avatar
+ 1
Thanks a lot for your help.....I am learning a lot because of u......thanku so much
17th Jan 2017, 3:34 PM
Akshay
Akshay - avatar
0
When I write var=input () print (var) if I type akshay I am getting the error as name ' akshay ' is not defined
17th Jan 2017, 3:34 PM
Akshay
Akshay - avatar