小晶君 发表于 2024-7-28 10:05:50

[原创]简单的Python代码:随机抽取

import random

spot_things_numbers = int(input("抽查数量:"))

spot_things_list = []

for i in range(spot_things_numbers):
    spot_things = input(f"请输入第{i+1}项:")
    spot_things_list.append(spot_things)

while True:
    output_things = random.choice(spot_things_list)
    print(f"抽到了:{output_things}")
   
    choice = input("是否继续抽取?(t/f):").lower()
    if choice == 'f':
      break
      
    remove_choice = input("是否删除抽取过的项目?(t/f):").lower()
    if remove_choice == 't':
      spot_things_list.remove(output_things)
      print(f"已删除项目:{output_things} 剩余项目数量:{len(spot_things_list)}")
      
    add_spot = input("是否添加更多项目?(t/f):").lower()
    if add_spot == 't':
      spot_things_numbers = int(input("重新添加多少项?:"))
      for i in range(spot_things_numbers):
            spot_things = input(f"请输入第{i+1}项:")
            spot_things_list.append(spot_things)
      print(f"已添加{spot_things_numbers}项,现有{len(spot_things_list)}项,重新开始...")
    else:
      print("重新开始...")
页: [1]
查看完整版本: [原创]简单的Python代码:随机抽取