服务器上班时分配一个随机字母

时间:2019-07-17 01:21:26

标签: python

这是针对python

import random


def main():

    user = input("are you a GO or SP?")

    if user == 'go' or user == 'GO':
        print("Thank You ")

    elif user == 'sp' or user == 'SP':
        print("You will be in section ")

    else:
        print("Welcome")


def section():

    list1 = ['a', 'b', 'c', 'd', 'e']

    b = random.randint(0, 5)

    (list1[b])

main()

section()

当前,当GO时钟为它分配一个部分时,我只需要为SP分配一个部分。

1 个答案:

答案 0 :(得分:3)

只需使用random.choice来选择一个部分。

import random

def main():

    user = input("are you a GO or SP? ").lower()

    if user == 'go':
        print("Thank You ")

    elif user == 'sp':
        sect = section()
        print(f"You will be in section {sect}")

    else:
        print("Welcome")


def section():

   return random.choice(['a', 'b', 'c', 'd', 'e'])

main()

尽管随机分配可能效果不佳,因为从理论上讲,它们都可能位于同一部分。