這裡有一個簡單的示例代碼,可以生成重複的兩位英文或數字:

import random
import string

# 創建一個包含所有英文字母和數字的列表
chars = string.ascii_letters + string.digits

# 隨機生成兩個字符
char1 = random.choice(chars)
char2 = random.choice(chars)

# 如果兩個字符不相同,就繼續生成
while char1 == char2:
    char2 = random.choice(chars)

# 打印結果
print(char1 + char2)

這段代碼首先創建了一個包含所有英文字母和數字的列表,然後使用 random.choice 函數隨機從列表中選擇兩個字符。如果兩個字符相同,則使用 while 循環繼續生成,直到兩個字符不相同為止。最後打印出結果。

python隨機生成重複的兩位英文或者數字

原文地址: https://www.cveoy.top/t/topic/gIRl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录