Python Basic Code Training

Source: Internet
Author: User

I. Create and export the menu, the menu is immutable. So use tuples
menus = ("1, Input", "2, Query", "3, delete", "4, Modify", "5, exit")
Store user's information ID: {' name ': ' Name ', ' weight ': weight, ' height ': height}
For example: There are currently two user information: 1. Wang Feng, 2. Zhang ziyi
Storage structure:
{
1:{' name ': ' Wang Feng ', ' weight ':, ' height ': 1.8, ' BMI ': 24.7},
2:{' name ': ' Zhang Ziyi ', ' weight ': ' Height ': 1.65, ' BMI ': 18.4}
}
Bodies = {}

body_id = 1 # numbering starting from 1

Body mass index (BMI) = weight (kg) ÷ (height (m) x height (m))
Weight unit: KG
Unit of Height: m
requirements: first. The Print menu, and then the user enters the selected menu item
Input 1: Enter the entry link. Users need to enter: name, height, weight.
The BMI index is calculated by the program. Save to the Bodies dictionary. The first user's ID is 1, the second is 2, and so on .
After the entry is complete. Prompts the user to continue the entry. If you choose Yes, continue with the entry until the user enters No. Return to main Menu
Enter 2: Enter the query link, prompting the user to enter the ID of the person to be queried. If it does not exist, give the hint if it exists. Displays all information about the user (first name, height, weight, BMI)
The user is then prompted whether to continue the query. If you choose Yes, continue with the query until the user enters no, return to the main menu
Enter 3: Enter the delete link, prompting the user to enter the ID of the person to be deleted, if the ID does not exist, give a hint, if present, perform the delete operation. and prompt to delete successfully.
then prompt the user whether to continue the deletion, if yes, continue to let the user select the ID to delete until the user enters no, return to the main menu
Input 4: Enter the modification link, first let the user enter the ID of the person to be modified, according to the ID to find the user information, if not present, give and prompt, if present, the user original information to print,
The user is then prompted to enter a new name, height, and weight. The BMI index is recalculated by the program. and save the new information in bodies. At the same time to show users new user information
The user is then prompted whether to continue with the modification, and if so, continues to require the user to enter the ID information. Until the user enters no, return to the main menu.
Enter 5: The program exits.
enter anything else. It is not legal to be prompted. Let the user re-enter

1Bodies = {}#Store users ' information2body_id = 1#numbering starting from 13  while1:4Menus = ("1, input","2, Query","3, delete","4, modify","5, Exit")5     Print("Main Menu", menus)6content = Int (input ('Please enter the menu options as required:'))7     ifContent = = 1:8          while1:9name1 = input ("Please enter your name:")TenHEIGHT1 = input ("Please enter your height:") OneWEIGHT1 = input ("Please enter your weight:") AB = Int (weight1)/(int (height1) * * 2) -             ifbody_idinchBodies.keys (): -                 Print("the ID you entered already exists") the             Else: -BODIES[BODY_ID] = {'name': Name1,'Weight': Weight1,'Height': Height1,'BMI': B} -                 Print("your ID is:", body_id) -                 Print(Bodies) +body_id + = 1 -con = input ("If you continue to enter, press q/q to Exit to the main menu and any key to continue:") +             ifCon.upper () = ='Q' : A                  Break at     elifContent = = 2: -          while1: -body_id = Int (input ("Please enter the ID you want to query:")) -             ifbody_idinchBodies.keys (): -                 Print(bodies[body_id]) -             Else: in                 Print("The ID you entered does not exist") -con = input ("whether to continue the query. If you choose Yes, press any key. Back to main menu, press q/q") to             ifCon.upper () = ='Q': +                  Break -     elifContent = = 3: the          while1: *body_id = Int (input ("Please enter the ID you want to delete:")) $             ifbody_idinchBodies.keys ():Panax Notoginseng Bodies.pop (body_id) -             Else: the                 Print("The ID you entered does not exist") +con = input ("whether to continue the deletion. If you choose Yes, press any key. Back to main menu, press q/q") A             ifCon.upper () = ='Q': the                  Break +     elifContent = = 4 : -          while1: $body_id = Int (input ("Please enter the ID you want to modify")) $             ifbody_idinchBodies.keys (): -name1 = input ("Please enter your name:") -HEIGHT1 = input ("Please enter your height:") theWEIGHT1 = input ("Please enter your weight:") -B = Int (weight1)/(int (height1) * * 2)WuyiBODIES[BODY_ID] = {'name': Name1,'Weight': Weight1,'Height': Height1,'BMI': B} the             Else: -                 Print("The ID you entered does not exist") Wucon = input ("whether to continue the modification. If you choose Yes, press any key. Back to main menu, press q/q") -             ifCon.upper () = ='Q': About                  Break $     elifContent = = 5: -          Break -     Else: -         Print("Roll Duzi, press the menu above to")

Two:

Functional Requirements:
Require users to enter their own 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.
goods = [
{"Name": "Computer", "Price": 1999},
{"Name": "Mouse", "Price": 10},
{"Name": "Yacht", "Price": 20},
{"Name": "Beauty", "Price": 998},
]
1Goods = [2{"name":"Computer"," Price": 1999},3{"name":"Mouse"," Price": 10},4{"name":"Yacht"," Price": 20},5{"name":"Beauty"," Price": 998},6 ]7content = Int (input ("Please enter your total assets:"))8Count = 1#Product serial Number9Buy=[]#Add Item number to CartTensum =0 OneA="' A  while1: -      forIinchGoods: -         Print("Product serial Number:", Count,i) theCount + = 1 -  -      whileA.upper ()! ='Q': -Buy.append (int (Input ("Please enter the item number you want to buy"))) +          forIinchBuy: -sum = sum + goods[i-1][" Price"] +         ifSum <=content: A             Print("Purchase Success") at         Else: -             Print("Insufficient account balance") -A = input ("continue pressing any key, exit please press q/q") -     Else: -          Break



Python Basic Code training

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.