Simple input number + list programme, but how to use strings? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Simple input number + list programme, but how to use strings?

Created my first programme. I wanted something with user-input that combined some early themes I've learned. I'm sure it's possible to define the order of print values by user-input too, which I may work on next. I'd like to know from the community, how it could be more sophisticated / eloquently written, and how I can use strings? At the moment it only works with numbers. a = input ("Enter number: ") b = input ("Enter number: ") c = input ("Enter number: ") list = (a, b, c) print list[0], list[1], list[2]

20th Jul 2017, 8:01 PM
Russell Dyson
Russell Dyson - avatar
9 Answers
0
Well... it actually is using stings. When you use input, it captures a string. So if you were to try and add these values together(I added a piece to your code as a test) you would get a concatenated string of numbers. So 1 + 2+ 3 would not be 6, but 123. You have to change the input to an integer as you bring in the code. Your code: a = input("Enter number: ") b = input("Enter number: ") c = input("Enter number: ") list = (a, b, c) test = a + b + c print(list[0], list[1], list[2]) print(test) Try with this as input: int(input("Enter number: "))
20th Jul 2017, 8:24 PM
Jim Tully
Jim Tully - avatar
0
a = input ("Enter number: ") b = input ("Enter number: ") c = input ("Enter number: ") myList = (a, b, c) print(myList) You shouldn't name your list "list"
20th Jul 2017, 8:17 PM
LordHill
LordHill - avatar
0
Thanks for this Joker and Jim. I think when I tried entering an alphanumerical input, like "Bob", the console returned an error (Python 2.7 on Ubuntu) that 'Bob had not been defined'. This really threw me, so I tested with numbers only, which worked and so I assumed it didn't work with strings. I don't have a reliable web connection at the moment so I can't really web search the solutions, which I'd normally do before posting.
20th Jul 2017, 8:49 PM
Russell Dyson
Russell Dyson - avatar
0
Jim, thank you. All your suggestions work brilliantly, and it does work with strings (although as you say they're already strings). Enter number: bob Enter number: gaz Enter number: jim bob gaz jim bobgazjim ;-)
20th Jul 2017, 9:02 PM
Russell Dyson
Russell Dyson - avatar
0
...although if I use your example: int(input("Enter number: ")) ...and I add a variable at the start: a = int(input("Enter number: ")) b = int(input("Enter number: ")) ...then entering a value like 'bob' returns an error. Yet if I use: a = input("Enter number: ") b = input("Enter number: ") ...then a value like 'bob' works fine. Why is this? Thanks.
20th Jul 2017, 10:03 PM
Russell Dyson
Russell Dyson - avatar
0
Ah, because once we told it to be an integer it can't process a string. So in most situations you are either dealing with a computation or a string. What is the ultimate goal of your process, to modify a string or calculate a value? You could put something in place to determine what you entered and handle it accordingly, but in most situations that isn't required.
21st Jul 2017, 3:23 AM
Jim Tully
Jim Tully - avatar
0
I'm simply trying to create a useful (?!?) implementation of some concepts and themes learned so far, because I learn best by jumping in at the deep end and just doing it. I suppose, I'd like to create something like a word-jumble game where the user can enter any value(s) and then choose the order at which they are returned, or have it randomised like bingo.
21st Jul 2017, 6:57 AM
Russell Dyson
Russell Dyson - avatar
0
I just figured out that I can replace int with str, which works for strings. I wonder if there is a way that I can determine the kind of input with an if /elif loop. Just pondering...
21st Jul 2017, 5:32 PM
Russell Dyson
Russell Dyson - avatar
0
You could also use the for loop to print the variables in list, but it would be outputted on separate lines. For example: a = input ("Enter number: ") b = input ("Enter number: ") c = input ("Enter number: ") list = (a, b, c) for num in list: print (num) If you inputted 1 as a, 2 as b, and 3 as c it would output: 1 2 3
23rd Jul 2017, 1:49 AM
Brian C
Brian C - avatar