When randomizing a Python list, randomize it by the number of random numbers n (please look at the code...)

Asked 2 years ago, Updated 2 years ago, 69 views

I'm still an inexperienced student in coding. An error occurs when you try to randomly extract (redundant) n (1<=n<=18) from the list... In this case, I'd like you to tell me how to code.

import random

menu=["Bread", "Chicken", "Bulgogi", "Shrimp", "Cheese", "Lambi", "Tomato", "Ketchup", "Mayonnaise", "Tartar", "Fried potatoes", "Nuggets", "Cheese Stick", "Coke", "Orange Juice", "Coffee", "Milk"]

n=[random.randint(1, 18)]

m=[random.choice(menu) for i in range(n)]

print(m)

Here's the code for the part that's in question.

According to the error message, it says TypeError: 'list' object cannot be interrupted as an integer, should n not be an integer? If you put integers such as 2 and 3 in the place with n, it would work well. I don't understand well, but if you can, I'd appreciate it if you could explain it further.

python list typeerror

2022-09-20 14:56

1 Answers

of the code for I in range (n) in range (n) of n this list . range 는 정수를 인자로 받는데, 그 부분에 정수가 아닌 리스트를 넣었기 때문에, 발생하는 에러입니다.

It's a problem that can be solved enough if you interpret and read the error message and think about what you mean.


2022-09-20 14:56

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.