Reputation: 51
New user practicing lists and loops. My goal is create two list from my own input (range is 2 for quick testing, but want to get to 10). I have hit a wall on two problems:
First Name \t\t\t $77
Next Name \t\t\t $16
My code:
list1 = []
list2 = []
count = 0
for i in range(2):
customers = input("Customers name? ")
list1.append(customers)
spent = float(input("Amount spent? "))
list2.append(spent)
count += spent
averageSpent = count / 2 # change this when changing range
print("Name\t\t\tAmount")
# won't print side by side. how to use zip()?
print((*list1 + list2), sep = "\n")
print("Total spending:\t\t $", count)
print("Average spending:\t $", averageSpent)
# keep getting 'object is not iterable'
#print("Most money spent:\t $", max(spent))
#print("Least money spent:\t $", min(spent))
My output is currently:
Customers name? work
Amount spent? 45
Customers name? please
Amount spent? 65
Name Amount
work
please
45.0
65.0
Total spending: $ 110.0
Average spending: $ 55.0
Thank you!
Upvotes: 0
Views: 11359
Reputation: 118
Regarding the first issue that you have, the best way for you to print the output side by side would be to iterate over the list and print each value in the list. In addition to that, you can use f-strings which is a feature that we added with python 3.6 and it allows you to do stuff like this:
x = 10
print(f'{x+20}')
>> 30
you can read more about f-strings here.
Regarding the second issue that you're facing. You got this error because you were calling the max() function on a single float. the max function should be called for a list.
I've made some adjustments. Here is what the final code looks like:
list1 , list2 = [] , []
max_range = 2
count = 0
for i in range(max_range):
customer_name = input("Customer name: ")
list1.append(customer_name)
spent = float(input("Amount Spent: "))
list2.append(spent)
count += spent
averageSpent = count / max_range
print("Name\t\t\tAmount")
for i in range(len(list1)):
print(f'{list1[i]} \t\t\t$ {list2[i]}')
print("Total spending:\t\t $", count)
print("Average spending:\t $", averageSpent)
print("Most money spent:\t $", max(list2))
print("Least money spent:\t $", min(list2))
Edit: using the zip function might be another option for printing the two outputs side by side using tuples. However, since you've already said that you're new to these topics I think that you should stay away from zip (for the time being) until you feel comfortable with lists.
Upvotes: 1
Reputation: 140
bikemule is correct about the iterables. You need to call max
with a list, not a single number. (What would max(101)
return? It doesn't quite make sense.)
To make the two lists appear side-by-side, you can use zip
combined with a for
loop. It will turn the lists into sets of tuples that then will appear in an almost-tabular format when printed.
list_a = [1, 2, 3]
list_b = [4, 5, 6]
for col_a, col_b in zip(list_a, list_b):
print("A_Item: %s | B_Item: %d" %(col_a, col_b))
Returns:
A_Item: 1 | B_Item: 4
A_Item: 2 | B_Item: 5
A_Item: 3 | B_Item: 6
Upvotes: 0
Reputation: 325
Printing things side by side is answered here: Print 2 lists side by side
The error about a float or int not being iterable is because you are calling max(spent)
instead of max(list2)
. The function max()
expects a list or other iterable object.
Upvotes: 1