商品价格竞猜简易版(每天一个python小项目)

list = [['小米手环4',209],['荣耀手环5',199],['华为手环B5',849],['ZNNCO智能血压手环',379]]
order = 0
price = 0
print("数字猜谜游戏!")
print('可以竞猜的商品如下:\n','1',list[0][0],'\n 2',list[1][0],'\n 3',list[2][0],'\n 4',list[3][0])

while True:
    number = input("请输入竞猜商品前面的数字:")
    if number.isdigit() == True:
        order = int(number)
        if order <= 4 and order > 0:
            print("您选择的竞猜商品是:",list[order-1][0])
            price = list[order-1][1]
            break
        else:
            print("您输入的数字有误,请重新输入!")
            continue
    else:
        print("您输入的不是数字,请重新输入!")
        continue

guess = 0
time = 0
while guess != price:
    guess =  input("请输入竞猜价格(只能输入整数价格):")
    time += 1
    if guess.isdigit() == True:
        guess=int(guess)
        if guess == price:
            print("恭喜!您猜中的奖品是:%s,一共猜了%d次"%(list[order-1][0],time))
        elif guess < price:
            print("猜的价格小了...")
        elif guess > price:
            print("猜的价格大了...")    
    else:
        print("输入价格非法,请重新输入!")    

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值