博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WEEK2---购物车
阅读量:6238 次
发布时间:2019-06-22

本文共 1539 字,大约阅读时间需要 5 分钟。

 
1 product_list = [ 2     ('iphone',5800), 3     ('Mac pro',9800), 4     ('Bike',800), 5     ('Watch',10600), 6     ('Coffee',34), 7     ('Book',120), 8 ] 9 shopping_list = []10 salary = input("Input your salary:")11 if salary.isdigit():12     salary = int(salary)13     while True:14         for index,item in enumerate(product_list):15             print(index,item)16         user_choice = input("choose what you want>>>:")17         if user_choice.isdigit():18             user_choice = int(user_choice)19             if user_choice < len(product_list) and user_choice >=0:20                 p_item = product_list[user_choice]21                 if p_item[1]<=salary:#买的起22                     shopping_list.append(p_item)23                     salary -= p_item[1]24                     print("Added %s into shopping cart,your current balance is \033[31;1m%s\033[0m"%(p_item,salary))25                 else:26                     print("\033[41;1m你的余额只剩[%s],还买个毛线\033[0m"%salary)27             else:28                 print("product code [%s] is not exist!"%user_choice)29         elif user_choice == 'q':30             print("-------------shopping list---------------")31             for p in shopping_list:32                 print(p)33             print("Your current balance:",salary)34             exit()35         else:36             print("invalid option")
购物车code

 

isdigit()方法的语法:
str
.isdigit()
如果字符串中的所有字符都是数字,并至少有一个字符此方法返回true,否则返回false。
View Code

运行上面的程序,它会产生以下结果:

True
False

转载于:https://www.cnblogs.com/piscesLee/p/7085916.html

你可能感兴趣的文章