You can use join, but join expects strings. I get around the issue by not converting the user input to numbers. I need to do a conversion to sum the numbers, but I don't keep the results around after I am done doing that. map(func, iterator) calls func(value) for each value in the iterator. Here I use it to convert my number strings to floats so they can be summed. The conversion will likely raise an exception, so I catch the exception and remind the user to input numbers.
user_inputs = [
input("Enter first number "),
input("Enter second number "),
input("Enter third number ")]
try:
total = sum(map(float, user_inputs))
except ValueError:
print("Not all inputs are numbers")
total = 0
average = total / len(user_inputs)
print("The total is ", total)
print("The average is ",average)
print("The input numbers are", ', '.join(user_inputs))
This can easily be made to work with any number of inputs. Here I use split(separator) to split the user input string into a list of substrings.
user_inputs =input('Enter numbers separated by "," ').split(',')
try:
total = sum(map(float, user_inputs))
except ValueError:
print("Not all inputs are numbers")
total = 0
average = total / len(user_inputs)
print("The total is ", total)
print("The average is ",average)
print("The input numbers are", ', '.join(user_inputs))
Of course if you really want the inputs as numbers you can do that too. For average I need a list of numbers so I can get the len() of the list. map() returns an iterator, and iterators don't have len() because they could potentially iterate forever. I use the list() function to extract all the values from the iterator and put them in a list.
try:
user_inputs = list(map(float, input('Enter numbers separated by "," ').split(',')))
except ValueError:
print("Not all inputs are numbers")
user_inputs = [0]
total = sum(user_inputs)
average = total / len(user_inputs)
print("The total is ", total)
print("The average is ", average)
print("The input numbers are", ', '.join(map(str, user_inputs)))
Since user_inputs are now numbers I need to convert them back to strings for the join function.