|
发表于 2024-10-28 21:13:29
|
显示全部楼层
- import random
- # 商品列表
- products = [
- {"name": "商品A", "price": 100},
- {"name": "商品B", "price": 200},
- {"name": "商品C", "price": 300},
- {"name": "商品D", "price": 400}
- ]
- # 随机选择一个商品
- chosen_product = random.choice(products)
- print(f"请猜以下商品的价格: {chosen_product['name']}")
- # 猜价格
- attempts = 0
- max_attempts = 20
- while attempts < max_attempts:
- guess = int(input("请输入你猜的价格: "))
- attempts += 1
- if guess > chosen_product['price']:
- print("价格高了,请继续竞猜")
- elif guess < chosen_product['price']:
- print("价格低了,请继续竞猜")
- else:
- print("恭喜你,你猜对了该商品的价格,你是大赢家!")
- break
- else:
- print("竞猜失败,下次再战!")
复制代码
确保在运行这个程序时,正确输入整数,否则会出现异常。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|