+ 1

I want to input string type and integer type

I want to get an input from the user, and save it in a variable as a string, and get an input from the user, and save it in a variable as an integer. but I get and Error name=input("Tell me your name:") age=input("Tell me your age:") print('name[0]') Can you tell me the problem?

21st Dec 2016, 10:32 AM
Maryam Sadeghi
Maryam Sadeghi - avatar
5 ответов
+ 5
You can print the first letter by simply doing print(name[0]). Python treats all the output from the input function as strings. You can use the int function to convert this string to a number like so: int(input("Your age:")). This will raise an exception if the input cannot be converted into a number.
21st Dec 2016, 11:13 AM
James Durand
James Durand - avatar
+ 4
The third line... Don't quote your var name ( however it's print the litteral string in it, in this case "name[0]" ) and remove the squares brackets and the 0 ( this syntax is for accessing arrays ) like this : print(name) ... so, it print the content of var name wich is your first entry. the var age isn't actually used... you can, like this : print(age) Well, if you want to use arrays, you can rewrite like this : rep=[] rep.append(input("Tell me your name:")) rep.append(input("Tell me your age:")) print(rep[0]) print(rep[1]) I suggest too to add a space or a new line at the end of your questions strings, for best view effect ;)
21st Dec 2016, 11:01 AM
visph
visph - avatar
+ 2
What error are you seeing? Syntactically the code looks correct however the print statement doesn't make much sense.
21st Dec 2016, 10:59 AM
James Durand
James Durand - avatar
0
thank you for the answers. what I want to do is to print the first letter of the name that is entered. and also when the user inputs text or numbers, does python know by default wether to save them as integer or string?
21st Dec 2016, 11:10 AM
Maryam Sadeghi
Maryam Sadeghi - avatar
0
@James Thanks a lot. this totally answered my question
21st Dec 2016, 11:15 AM
Maryam Sadeghi
Maryam Sadeghi - avatar