samedi 26 février 2022

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

Can anyone help me figure out how to print elements of a randomly created list next to chosen numbers in order? The numbers are book names and the numbers that are random are book prices and I need those prices to be returned next to book names after you have chosen what books you are going to buy

Here is my code:

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)}')

It returns something like this:

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

I would like it to be more like:

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



Aucun commentaire:

Enregistrer un commentaire