简体   繁体   中英

Print pairs of values from two lists

Everything is working for me in this program except for one thing; I need my output to group the names and values together in the print(names, "\\n" values) section.

For example: Pat : €99, Kev : €55, Dermot : €100 .

My code is below:

salesperson = int(input("How many salesperson's do you wish to account 
for:"))
names = []
values = []
counter = 0
total_sales = 0
total_value = 0
if salesperson > counter:
    while salesperson != counter:
        name = input("Please enter in the salesperson's name:")
        value = float(input("Please enter in the value of sales for the 
        salesperson:"))
        salesperson -= 1
        names.append(name)
        values.append(value)
        total_value += value
        from statistics import mean
        average_values = mean(values)
        minimum = min(values)
        maximum = max(values)
    print(names,"\n",values)
    print(average_values)
    print(total_value)
    print(minimum)
    print(maximum)

You can do this using zip() , and f-strings :

print([f"{x}: £{y}" for x, y in zip(names,values)])

Output:

['Pat: £1.0', 'Dermot: £2.0', 'Kev: £3.0', 'Boris: £4.0', 'Jeremy: £5.0']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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