简体   繁体   English

如何水平打印收集到字典中的多个输入

[英]How to print multiple inputs that are gathered into a dictionary horizontally

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.该程序需要 3 个输入:名称、数量和价格。 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.然后它会询问您是否要使用通常的 y 或 n 输入更多,然后再输入另一个 3,依此类推。

However, the print() will only print the last 3 inputs instead of all 3 sets.但是,print() 只会打印最后 3 个输入,而不是所有 3 个集合。

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 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()有没有 function 我需要像 print a.function() 这样的打印函数

or is it something in the formatting of the dictionary?还是字典格式中的某些内容?

(edit:sorry this board has weird mechanics) (编辑:抱歉这个板有奇怪的机制)

I am bit confused why you want to print same key value pair 3 times in one line.我有点困惑为什么要在一行中打印 3 次相同的键值对。 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])
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)

So I figured it out and forgot to come back.于是想通了,忘记回来了。 All I had to do was ADD the value to the key.我所要做的就是将值添加到键中。

So it would be like:所以它会像:

a[name]= count

d[name]= price

This gave me the loops I needed to fill out the whole thing.这给了我填写整个内容所需的循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM