Python & lt; --- & gt; small topic exercises, python ---

Source: Internet
Author: User

Python <---> small topic exercises, python ---

Job 1:

With the * dictionary * output directory, you can select a directory to enter. You can roll back and exit!

# Conding: utf8


Menu = {'beijing': {'changping ': {'shahe': {'changping maternal and children ', }}, 'haidian': {'haidian 1 ': {'haidian 2's },},}# dictionary nesting

Current_layer = menu # use this variable to record the location of the current Layer
Pre_menu = [] # use [] to record each layer

While True:
For k in current_layer:
Print (k)
Choice = input ('>>>>>>>:'). strip () # Remove the spaces of the input options.
If len (choice) = 0: # Use The len Length to determine whether the user has entered
Continue

If choice in current_layer:
Pre_menu.append (current_layer)
Current_layer = current_layer [choice]
Elif choice = 'B ':
If len (pre_menu)> 0: # if len (pre_menu) is greater than 0, a directory can be returned.
Current_layer = pre_menu.pop () # Use pop to pop up the above layer.
Elif choice = 'q ':
Exit ()

Assignment 2

Small list exercise: implements the shopping cart Function

Product_list = [['iphone ', 6500], ['mobli', 5000], ['huawei', 4000], ['oppo ', 300], ['vivo ', [100]


Money = int (input ('Please input your money :'))
Got_list = []

While True:
For I, v in enumerate (product_list): # display a two-dimensional series, 0... 1... 2 ....
Print (I, v)
Choice_goods = input ('which good do you want to get :')

If choice_goods.isdigit (): # determine whether the number is input.
Choice_goods = int (choice_goods)
If choice_goods <len (product_list) and choice_goods> = 0: # condition judgment to check whether the condition is out of the range
If money> product_list [choice_goods] [1]: # if the money is higher than the price of the item
Money-= product_list [choice_goods] [1] # Calculate the balance
Got_list.append (product_list [choice_goods] [0]) # Add the purchased item to the list
Print ('You good is % s' % (got_list ))
Print ('You left money is: % s' % (money ))
Print ('')
Print ('')

Else:
Print ('You do not have enough money to bu anything! ')
Print ('\ n ')

Else:
Print ('we do not have this good ')
Print ('\ n ')

Assignment 3

Bubble algorithm small exercise

A_list = [9, 8, 7, 6, 5, 4, 3, 2, 1]

For j in range (len (a_list)-1): # in the second step, you need len (a_list)-1 to arrange a number in each loop.
# Step 1: The primary algorithm does not subtract j, and each cycle is compared from start to end. If j is subtracted, no comparison is required for the following sorting.
For I in range (len (a_list)-1-j ):
If a_list [I]> a_list [I + 1]: # maximum number of rows to the end
A_list [I], a_list [I + 1] = a_list [I + 1], a_list [I] # use a, B = B, a to call

Print (a_list)

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.