The solution I provided makes use of Python 3, which you appear to be using.
#!/usr/bin/env python3
"""The segment below takes user input for your list."""
userInput = input("Enter a comma-delimited list of numbers \n")
userList = userInput.split(",")
try:
data_list = [float(number) for number in userList]
except ValueError:
print("Please enter a comma-delimited list of numbers.")
def list_modifier(list_var):
"""Print the original list, average said list, modify it as needed,
and then print again."""
print("Your list is: " + list_var)
average = sum(list_var) / len(list_var) # Divide list by sum of elements
for x in list_var:
x += 1
print("Your modified list is: " + list_var)
list_modifier(data_list)
I used a little bit of fancy stuff to take the user input that handles errors if they arise, but really you should not be worrying about stuff like that.
The split() string method just splits up strings into a list of individual smaller strings using whatever parameter you give it. In this case, I split it up on every comma. I added error handling because if you end a string in a comma, the input function will not work.
I also made use of list comprehension, a type of expression in python that creates lists based on the parameters passed within the brackets. This is seen in the code below:
[float(number) for number in userList]
This takes each string within the list of strings created by split() and makes it into a number. We now have our desired list of numbers.
Beyond that, we have the list_modifier function, which first states the list of numbers with string concatenation. It then uses the sum() function to find the sum of all of the elements of the list and divides that sum by the length of the list.
The for code block takes each element in the list and adds one to it. String concatenation is used once again to finally show our modified list.
I really hoped this solution helped, sorry if it was a bit overly-complex. Try/except blocks can be quite useful for handling errors, I really recommend you look into them later on. See the Python documentation as well if you want to get ahead in class.
Good luck and have fun!
sum()function that takes a sequence.