3 回答

TA贡献1825条经验 获得超4个赞
解决这个问题的一种方法是制作一堆 if then ,如下所示:
if rand_let == 1:
print("A")
另一种简单的方法是制作一个字母列表,然后使用 rand_let - 1 索引请求字母(因为列表中的第一个东西的索引为 0。
LetterList = ["A", "B", "C", "D", "E", "F", and so on...]
letter = LetterList[rand_let-1]
print(letter)

TA贡献1777条经验 获得超10个赞
有几种方法可以做到这一点。首先,您可以创建一个字典并将字母指定为数字。另一种方法是找到 chr() 代码编号,然后使用随机数生成器来选择字符编号。这是我推荐的。
# imports
import random
# pick a random number between the ord of 'a' and the ord of 'z' Google these for
# more info
pick = random.randint(ord('a'), ord('z'))
# print the char of the ord of the random number picked
print(chr(pick))

TA贡献1817条经验 获得超6个赞
chr()是你的转换器功能。偏移量为 64,因为要使用的数字来自 ASCII 字符表(即:A = 65,B = 66,...):
# import random
# rand_let = random.randint(1,26)
# print(rand_let)
rand_let = chr(rand_let + 64)
# print(rand_let) # This is the changed version
添加回答
举报