Home > OS >  How to print multiple inputs that are gathered into a dictionary horizontally
How to print multiple inputs that are gathered into a dictionary horizontally

Time:03-28

Im trying to figure out how to print multiple inputs from one dictionary. The way i have it, it only prints the last input sequence instead of printing all three inputs.

For reference:

The program asks for 3 inputs:name, count, and price. It then asks if you want to enter more with the usual y or n input and then you put in another 3, so on and so forth.

However, the print() will only print the last 3 inputs instead of all 3 sets.

What i have looks like this

input name
input count
input price
input more? y or n
a = {name:count}
d = {name:price}
print(a)
print(d)

output

'name',count
'name',price

but it should look like

{'name',count 'name',count 'name',count}
{'name',price 'name',price 'name',price}

is there a function i need to put on the print functions like print a.function()

or is it something in the formatting of the dictionary?

(edit:sorry this board has weird mechanics)

CodePudding user response:

I am bit confused why you want to print same key value pair 3 times in one line. However if you really need that you can print

print(a,a,a)
print(d,d,d)

You cant create a dict with duplicate keys, but one key can have multiple values like

a = {name: [count, price]}

And I think most logical would be

a = {name: name, count: count, price: price}
print("name ", a[name], ", count ", a[count], ", price ", a[price])

CodePudding user response:

def ask_questions(name_list, count_list, price_list):
    name_list.append(input("name: "))
    count_list.append(input("count: "))
    price_list.append(input("price: "))

name_list = []
count_list = []
price_list = []

While input("more? y or n: ") == "y":
    ask_questions(name_list, count_list, price_list)

a = dict(zip(name_list,count_list))
d = dict(zip(name_list,price_list))
print(a)
print(d)
  • Related