以下是使用Python编写的抢购数学藏品的代码:

import random

math_collection = {
    '1': 'Fermat's Last Theorem',
    '2': 'The Riemann Hypothesis',
    '3': 'The Goldbach Conjecture',
    '4': 'The Twin Prime Conjecture',
    '5': 'The Collatz Conjecture'
}

def buy_math_collection():
    print('欢迎来到数学藏品抢购活动!')
    while True:
        choice = input('请选择您要购买的数学藏品编号(1-5):')
        if choice in math_collection:
            price = random.randint(1000, 5000)
            print('恭喜您抢到了', math_collection[choice], '!')
            print('请支付', price, '元。')
            break
        else:
            print('您输入的编号有误,请重新输入。')

buy_math_collection()

代码说明:

  1. 定义一个字典math_collection,保存数学藏品的编号和名称。
  2. 定义一个函数buy_math_collection(),用于进行抢购活动。
  3. 在函数中使用input()函数获取用户输入的数学藏品编号。
  4. 如果用户输入的编号在math_collection字典中存在,则随机生成一个价格,并输出抢购成功的信息和支付金额。
  5. 如果用户输入的编号有误,则提示用户重新输入。
  6. 使用while循环,直到用户输入正确的编号为止。
Python代码:抢购数学藏品模拟

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

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