如何按顺序在其他元素旁边打印随机列表的元素?

How to print elements of a random list next to other elements in order?

谁能帮我弄清楚如何在所选数字旁边按顺序打印随机创建的列表中的元素? 这些数字是书名,随机数字是书价,在您选择要购买的书后,我需要在书名旁边返回这些价格

这是我的代码:

import random

print("Books for sale")

m = 25
price_list = []

for s in range(3, m + 1):
    price_list.append(s)

i = 0
books = 1
n = 10
book_list = []

while i < n:
  prices = random.sample(price_list, 1)
  print(f"{books}: {prices}")
  i += 1
  books += 1

print("Enter, which books are you going to buy (1 - 10) ==>")
numbers = [int(s) for s in input().split()]
print("Chosen books: ")

for el in range(len(numbers)):
    print(f'{numbers[el]}: {price_list.count(el)}')

它returns是这样的:

Books for sale
1: [8]
2: [25]
3: [5]
4: [24]
5: [12]
6: [24]
7: [16]
8: [3]
9: [21]
10: [13]
Enter, which books are you going to buy (1 - 10) ==>

2 5 7
Chosen books: 
2: 0
5: 0
7: 0

我希望它更像:

Chosen books: 
2: 25
5: 12
7: 16

您需要在某处保存价格。字典是一个很好的容器。

我也折射了一下代码:

import random
print("Books for sale")
m = 25
price_list = list(range(3, m + 1))
n = 10
books = {}
for book in range(n):
  price = random.sample(price_list, 1)[0]
  print(f"{book}: {price}")
  books[book] = price
print("Enter, which books are you going to buy (1 - 10) ==>")
numbers = [int(s) for s in input().split()]
print("Chosen books: ")
for n in numbers:
    print(f'{n}: {books[n]}')

输出:

Books for sale
0: 5
1: 10
2: 18
3: 11
4: 20
5: 25
6: 13
7: 23
8: 8
9: 18
Enter, which books are you going to buy (1 - 10) ==>
2 5 7
Chosen books: 
2: 18
5: 25
7: 23