future=random.randint(1,8)

if future == 1:
         answer = option1
elif future == 2:
         answer = option2
elif future == 3:
         answer = option3
elif future == 4:
         answer = option4
elif future == 5:
         answer = option5
elif future == 6:
         answer = option6
elif future == 7:
         answer = option7
else:
         answer = option8


我需要缩短/提高效率,任何帮助都会很棒,谢谢:)

最佳答案

为什么不将选项存储在列表中?

options = [option1, option2, option3, option4, option5, option6, option7, option8]
future = random.randint(0,7)
answer = options[future]

关于python - 如何使此elif语句更有效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35509973/

10-16 17:35