01 input输入的是str类型
如果输入的是数字的话,要记得强制转换一下!
02 isdigit()
这个方法是用来检测字符串是否全部由数字组成
str.isdigit()
如果字符串只包含数字则返回 True 否则返回 False。
03 购物车程序
'''程序:购物车程序
需求:
启动程序后,让用户输入工资,然后打印商品列表
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
可随时退出,退出时,打印已购买商品和余额'''
# -*- coding: utf-8 -*-
print("-------shopping_cart-------")
print("There is the goods and its price:")
flag = [1, 2, 3, 4, 5]
GoodsList = ["A", "B", "C", "D", "E"]
value = [50, 100, 150, 200, 250]//这里把这俩列表嵌套会更好一些!!
for i in flag:
a = GoodsList[i - 1]
b = value[i - 1]
print(str(i) + ". " + a + ", " + str(b))
Salary = input("please input your salary:")
LeftSalary = int(Salary)
AlreadyBuy = []
while True:
Number = input("please input your choice(input the number):")
if Number.isdigit() and int(Number) in range(1, 6):
if LeftSalary < value[int(Number) - 1]:
print("sorry, your money isn't enough")
else:
AlreadyBuy.append(GoodsList[int(Number) - 1])
LeftSalary = LeftSalary - value[int(Number) - 1]
print("you have %d left" % LeftSalary)
elif Number == "quit":
print("you have end the process,you have %d left" % LeftSalary)
print("you have bought:", AlreadyBuy)
break
else:
print("you have input the wrong commend!")