Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Implementation of simple Shopping screening by python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Request:

1, let the user enter how much money he has:

2. Output what the user can buy

3. Put the things bought by users on the list.

4. Calculate how much money the user still has until he cannot buy anything.

5. Users cannot return or exchange as long as they choose the product.

Python version 2.7

Code implementation:

#-*-coding: utf-8-*-products = ["book", "plane", "phone", "fan", "hen", "bed", "car"] price = [65,1000000 hen "," bed "," car "] price = [65,1000000 shop_list=] shop_list= [] salary = int (raw_input (" please input your money: ") while 1: print u 'Please buy one or more of the following items:' for i in range (0) Len (products): if (salary > = price [I]): print products [I], price [I] print "+-+" choice = raw_input ("please choice a shop to buy:") F_choice = choice.strip () # remove spaces Format the output. If F_choice in products: product_price = price [products.index (F_choice)] # take out the product price print "+-- +" print u "the goods you want to buy and the price:", F_choice "product_price print u" items are being added to the shopping list Please wait a moment "if salary > product_price: shop_list.append (F_choice) salary = salary-product_price print" +-+ " Print u "% s has been added to your shopping list"% F_choice print u "your balance still has:" Salary print u "the items you have purchased are:", shop_list print "+-+" else: pass else: print u "the items you entered are not in the list of items. Please re-enter it! "

If the item is in the file, you can use the following code to process it into a list:

#-*-coding:utf-8-*-products= [] prices= [] f = file ("jiage.txt") for line in f.readlines (): new_line = line.split () products.append (new_line [0]) prices.append (int (new_ line [1]) print productsprint prices

Execution result

The execution result of the final filter:

Summary:

Product_price = price [products.index (F_choice)] # take out the product price

This line of code python2.7 looks different from Python2.6.

Another way to write it is:

Product_price = print [products.index] # take out the product price

But the following mistakes will be reported, and the following is what I pasted: just change it to ().

TypeError: 'builtin_function_or_method' object has no attribute' _ _ getitem__'

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report