Python list and loop judgment

Source: Internet
Author: User
Tags python list

 lists and loops

__author__ = ' Yimtsao '
# Coding:utf-8
List = [
(' iphone ', 8050),
(' Mac Pro ', 12000),
(' Starbuck latte ', 31),
(' Alex Python ', 82),
(' Bike ', 800),
] #商品列表
Gouwuche = [] #空的购物车
While True:
Wage = input ("Please enter your existing amount:")
If Wage.isdigit (): #判断该字符串是否为数字
wage = Int (wage) #将由数字组成的字符串定义为整数
While True:
For list_tmp in list:
Print (List.index (list_tmp), list_tmp) #循环打印出商品列表
num = input ("Select the item number you want to buy:")
If Num.isdigit ():
num = int (num)
If num >= 0 and Num < len (list): #判断 The number entered is within the range of the product list number
Articles = List[num]
If wage >= articles[1]: #判断余额是否够买商品
WAGE-=ARTICLES[1] #购买商品后的余额
Gouwuche.append (articles) #将购买的商品加入购物车
Print ("You have already purchased%s, your current balance is%s"% (Articles,wage))
Else
Print ("Your balance is not enough, go make money, you still have", wage)
Else
Print ("Output wrong product number, please re-select")
else:
if num = = "Q": #输入为Q print balances and items and then exit
Print ("Your shopping cart has%s, your balance is%s"% (Gouwuche,wage))
Exit ()
Else
Print ("Your input is an invalid parameter, please reenter")
Else
If wage = = "Q":
Exit ()
Else
Print ("Your input error, please retry, if you need to exit Please enter Q")

--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------
First, the ugly code, which is the use of this three days to learn the knowledge written, the main function of the implementation of input your cash, the circulation of the product list, by product number shopping. Enter q at any step to exit and print the balance and the item you have already purchased.
In my writing, there are two difficult points: first, if you print out a list of items, I want to start with List = "" "" "and then" "" "" in the middle list out, but so that the printed list of goods and our creation of the list is not associated, want to maintain is also very troublesome, so this foolish idea is undesirable.
Finally, I print out the list elements and use List.index () to print the item number using the For I in List loop. The second difficulty is how to make the input string, if it is the number on the rounding, if not the number on the judge is not Q exit. Here I use the isdigit () to determine if the string is a purely numeric
Yes, the string is replaced with an integer, if not in the judgment is not Q exit.
These two points are done, the rest is simple if the judgment after the printing and operation, indentation is very important, in writing because the indentation problem debugging several times.


List
Lists are one of the most common types of data that can be used to make the most convenient storage, modification, and other operations.
Definition List
name=[' Yimtsao ', ' cnblogs ', ' China '
NAME[0]
Yimtsao
NAME[1]
Cnblogs
NAME[2]
China
Slices: Fetching multiple elements from a list
Name[0:1] #列表中下标从0到1的元素, not including the last one
' Yimtsao '
NAME[0:-1] #-1 said the last one
Name[:2] #放空表示从第一位开始
Name[0::2] #后面的2是代表, every other element, take a

Append
 >>> name[  " yimtsao  " ,  " cnblogs   " "  >>> name.append ( " new   " )  >>> name[   " yimtsao   ' ,  '  cnblogs   ' ,  '  new   ' ] 

Insert

>>> name['yimtsao'cnblogs'] >>> Name.insert (1,"new") Insert >>> name[in front of the next labeled 1   ' Yimtsao ' ' New ' ' Cnblogs ']

Modify

>>> name['yimtsao'cnblogs'] " New " assigns the element labeled 1 to the new value >>>  name['Yimtsao' New ']

Delete

del name[1]  remove elements of subscript 1 >>> name['yimtsao'] Name.remove ("yimtsao") specifies the element to delete ['new'  ]name.pop () Delete the last element of the list ['yimtsao',]

Expand

>>> name[' Yimtsao'new'> >>b = ['a','b']>>>  Name.extend (b) Add list B to name

Copy

>>>name['Yimtsao','New','a','b']&GT;&GT;&GT;NAME_CP =name.copy ()>>>name_cp['Yimtsao','New','a','b']

Statistics

Name.count ("") Number of statistics list elements >>>name['yimtsao'  new','a','b','  a']>>>name.count ("a") 2

Sort & Flip

>>>name['Yimtsao','Cnblogs', 1, 2, 3]>>> Name.sort ()#SortTraceback (most recent): File"<stdin>", Line 1,inch<module>typeerror:unorderable types:int ()< STR ()#3.0 miles Different data types cannot be sorted>>>name['Yimtsao','Cnblogs','1','2','3']>>>name.sort () ['1','2','3','Yimtsao','Cnblogs'] Name.reverse () ['3','2','1','Cnblogs','Yimtsao']

Get subscript

>>>name['yimtsao'cnblogs',' 1 ','1','3']>>> Print (Name.index ('1'# returns only the first small label found 2









Python list and loop judgment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.