Program Python untuk Memilih Secara Acak Elemen Dari Daftar

0
(0)

Contoh 1: Menggunakan modul acak

import random

my_list = [1, 'a', 32, 'c', 'd', 31]
print(random.choice(my_list))

Keluaran

31

Catatan: Outputnya mungkin berbeda.


Contoh 2: Menggunakan modul rahasia

import secrets

my_list = [1, 'a', 32, 'c', 'd', 31]
print(secrets.choice(my_list))

Keluaran

c

Ini secara kriptografis lebih aman daripada random modul.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.