-2

I'm working on a project where I have to create a Mean, Median, Mode, and Range calculator and I keep failing to get the number from the user as input. Here's the code:

print("Mean, Median, Mode, and Range Calculator")
user_input = input("Press 1 to choose Mean , 2 to choose Median, 3 to choose Mode, and 4 to choose Range")

def get_num():
    x = [input("Enter your numbers Without commas ie. 12 34 56: ")]
    x1 = []
    for i in x: 
        x1.append(int(i)) 
        print(x1)


if user_input == '1':
    pass

But I keep getting this error:

ValueError: invalid literal for int() with base 10: '12 34 56'

I've tried using a map, using for to loop through the list but it doesn't work. I don't even know what the error means can someone explain?

Barmar
  • 741,623
  • 53
  • 500
  • 612

3 Answers3

2

Try this. Also user_input is undefined. if you're using x1 just to convert to int then you don't need it. map does that for you.

def get_num():
    x = map(int,input("Enter your numbers Without commas ie. 12 34 56: ").split())
    return x


x1 = []
x = get_num()
for i in x:
    x1.append(i)
    print(x1)
pjk
  • 547
  • 3
  • 14
1

Don't put the call to input() inside a list. To get a list of the words that were entered, use split().

def get_num():
    x = input("Enter your numbers Without commas ie. 12 34 56: ")
    x1 = []
    for i in x.split(): 
        x1.append(int(i)) 
    print(x1)

You can also use a list comprehension.

def get_num():
    x = input("Enter your numbers Without commas ie. 12 34 56: ")
    x1 = [x1.append(int(i)) for i in x.split()]
    print(x1)
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

A nice way is to use a map to convert the input to int and use list function to wrap the map.

def get_num():
    x = input("Enter your numbers Without commas ie. 12 34 56: ")
    x1 = list(map(lambda i: int(i), x.split()))
    print(x1)