Python List

Asked 2 years ago, Updated 2 years ago, 73 views

Save 'grave', 'banana', 'gravefruit' and 'melon' as elements in the list named Fruit, mix them with random module functions, and then how do I write the first element to display the message 'correct' that is 'grave'?! Do you use random shuffle?

python random

2022-09-20 19:33

1 Answers

I don't know well, so I can't make the code neatly, but if I just make it turn around,

import random

list1 = ['grape' , 'banana' , 'grapefruit', 'melon']
list2 = []

list2.append(list1.pop(random.randrange(0,4)))
list2.append(list1.pop(random.randrange(0,3)))
list2.append(list1.pop(random.randrange(0,2)))
list2.append(list1.pop(random.randrange(0,1)))

if list2[0] == 'grape':
    print('correct')

It hasn't been long since I learned Python, but I was going to try something, so I posted a source code. What should I do about this? I think it'd be better if you ask questions like this.


2022-09-20 19:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.