Python Data type Exercises

Source: Internet
Author: User

First, the element classification

There is a collection of the following values [11,22,33,44,55,66,77,88,99,90 ...], saving all values greater than 66 to the first key in the dictionary, and saving the value less than 66 to the value of the second key.
That is: {' K1 ': All values greater than 66, ' K2 ': All values less than 66}

li=[11,22,33,44,55,66,77,88,99,90]dic={'K1':[ ],'K2':[ ]} forIinchLi:Print(i)ifI >70: dic['K1'].append (i)Else: dic['K2'].append (i)Print(DIC)
Find the elements in the lookup list, remove the spaces for each element, and look for all elements that begin with a or a and end with C. Li = ["Alec", "Aric", "Alex", "Tony", "Rain"] Tu = ("Alec", "Aric", "Alex", "Tony", "rain") dic = {' K1 ': "Alex", ' K2 ': ' Aric ', ' K3 ': ' Alex ', ' K4 ': ' Tony '}
Li = ["Alec","Aric","Alex","Tony","Rain"]tu= ("Alec","Aric","Alex","Tony","Rain") DiC= {'K1':"Alex",'K2':'Aric',"K3":"Alex","K4":"Tony"}ret= [] forIinchli:i=I.strip ()ifI.startswith ('a'or'A') andI.endswith ('C'): Ret.append (i) forIinchtu:i=I.strip ()ifI.startswith ('a'or'A') andI.endswith ('C'): Ret.append (i) forIinchdic:i=I.strip ()ifI.startswith ('a'or'A') andI.endswith ('C'): Ret.append (i)Print(ret)#results: [' Alec ', ' Aric ', ' Alec ', ' Aric ']
Three Output Product list, user input serial number, display the user selected productsProduct Li = ["Mobile phone", "Computer", "mouse pad", ' yacht ')
li=["Mobile Phone","Computer","Mouse Pad","Yacht"]Print("0 is a mobile phone, 1 is a computer, 2 is a mouse pad, 3 is a yacht") Num=input ("Please enter a number:")ifnum=='0':    Print(li[0])elifnum=='1':    Print(li[1])elifnum=='2':    Print(li[2])elifnum=='3':    Print(li[3])Else:    Print("')
four, shopping cart

Functional Requirements:

    • Require users to enter total assets, for example: 2000
    • Display the list of items, let the user select the item according to the serial number, add the shopping cart
    • Purchase, if the total amount of goods is greater than the total assets, indicating that the account balance is insufficient, otherwise, the purchase succeeds.
    • Add: Can recharge, a product to remove the shopping cart
Goods = [    {"name":"Computer"," Price": 1999},    {"name":"Mouse"," Price": 10},    {"name":"Yacht"," Price": 20},    {"name":"Beauty"," Price": 998},]Print("enter 0 to buy a computer, enter 1 to buy a mouse, enter 2 to buy a yacht, enter 3 to buy a beauty, enter another value, settle the cart") Li=[]total= Input ("Please enter the total amount:") whileTrue:num= Input ("Enter the serial number you want to purchase:")    ifint (num) = =0:li.append (goods[0][' Price'])    elifint (num) = = 1: Li.append (goods[1][' Price'])    elifint (num) = = 2: Li.append (goods[2][' Price'])    elifint (num) = = 3: Li.append (goods[3][' Price'])    Else:         Breaksum=0 forIinchLi:sum+=IPrint(sum)ifSum>int (total):Print("Insufficient Balance")Else:    Print("Purchase Success")

Python Data type Exercises

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.