0

I am trying to make a simple calculator that calculates basic statistic functions. I am almost done but I cannot figure out how to make a user input a list of numerical values. This is where I am currently at:

#inputList code?
inputList = []
numbers = input('Input your numbers: ').split(',')
for entry in numbers:
    inputList.append(entry)

This gives me the error: TypeError: can't convert type 'str' to numerator/denominator Here is a link to all the code: http://pastebin.com/B7u5a6LA

6
  • Aside from the fact that it's really just inputList = input(...).split(','), the code you have provided works fine for me. Please provide a minimal example that actually recreates the issue. Commented Oct 15, 2014 at 15:00
  • Aside: in your linked code you forgot a bunch of return statements. Unlike some languages, Python doesn't automatically return the result of the last line executed in a function. Commented Oct 15, 2014 at 15:04
  • @DSM yes yes, now it works! I have forgotten to do that. Silly me. Thanks everybody for your answers. Commented Oct 15, 2014 at 15:12
  • @avi thank you, I used the inputList.append(int(entry)) and now it works. Thank you very much! Commented Oct 15, 2014 at 15:13
  • @aruisdante thank you, for editing my text and providing the solution. ^-^ Commented Oct 15, 2014 at 15:15

2 Answers 2

2

Change those to int with int(some_string):

for entry in numbers:
    inputList.append(int(entry))

EDIT: As suggested by @tobias_k and @DSM, you can use map fucntions:

inputlist = list(map(int, numbers))
Sign up to request clarification or add additional context in comments.

4 Comments

Or shorter: inputlist = map(int, numbers)
added to the answer. Thanks for suggesting!
Careful: the question is tagged 3.x, and map doesn't return a list in 3. You'd need list(map(int, numbers)).
oops, great catch. edited the answer. My mind still hung upon python 2.
0

Depending on how robust to bad input you want to be, you can do the following in one line to get it done:

inputList = [float(num.strip()) for num in input("Enter a list of numbers: ").split(',')]

Note that the user entering something that wasn't a number, or something that wasn't a comma delimited list of numbers, will cause this to throw a ValueError. If that is something you're worried about.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.