Get the App
SLTechnology News&Howtos  ›  Servers  › 

Implementation of simple Shopping screening by python

Shulou Source: shulou.com Published: 2022-06-02 06:20:01 10月01日 Update

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__'

Tags: Product user price product code face input shopping thing writing result utf-8 output small balance file format current version space Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Apple Huawei Linux MariaDB