Day1_17-9-3,
I. Application Exercises 1. Requirement exercises
Write login interface
Requirements:
- Ask the user to enter the user name and password
- Welcome information displayed after successful authentication
- Enter an error three times and exit the program.
#! /Usr/bin/env python # coding: utf-8time = 0 data = {'hangsan ': {'ps': 'zs123'}, 'lisi': {'ps ': 'ls123'},} while time <3: name = input ('enter name: ') if not name in data: print ('Do not have this user ') continue password = input ('enter your password: ') if password = data [name] ['ps']: print ('Log in successfully') else: print ('user name or password error') time + = 1 else: print ('maximum times exceeded ')Code
Upgrade requirements
- Supports logon by multiple users
- After the user fails authentication three times, the user exits the program. When the user starts the program again and tries to log on again, the user is still locked.
# Storage user data = {'hangsan': {'ps': 'zs123'}, 'lisi': {'ps ': 'ls123'} # open the file named "user" in the same directory (read-only), # Give the read content to the variable # Check and close the file us1 = open ('user ', 'R') name = us1.read () us1.close () time = 0 while time <3: user = input ('your name >>> ') if not user: continue if user in name: print ('the account! ') Break # If the input name is the same as the content read in the file, the user is indicated as locked and a loop is launched. Elif not user in data: print ('without this user \ n') continue # If the input name is not the same as the known user name, the user is notified and jumps out of this loop. Else: passwod = input ('password >>> ') if passwod = data [user] ['ps']: print ('Log in successfully') exit () # If the password is correct, exit the script else: print ('user name or password error! \ N') time + = 1 # incorrect password input, tell the user and re-enter the user name and password, record times else: print ('maximum times exceeded, contact the postmaster ') record = open ('user', 'A') record. write ('% s \ n' % user) record. close () exit () # after reaching three times, tell the user to open the user file and append the user name that has been entered three times, and close the file normally.Code
The flowchart is as follows:
2. daily exercises
Briefly describe the differences between compilation and interpretation languages, and list which languages you know belong to the compilation type and which ones belong to the Interpretation Type respectively?
Compile-type Languages read all the content in one row, and execute all the content at one time. Advantages: Fast execution speed, low system requirements, and disadvantages: inconvenient code testing and troubleshooting, compilation languages are inconvenient to debug, including C/C ++ and Pascal/Object Pascal (Delphi) interpretation languages which are line-by-line translation and one line of execution. Advantages: convenient code debugging, timely Problem Discovery and timely solution disadvantages: relatively slow execution speed compared with compilation language Interpretation Language: python, Java, C #, JavaScript, VBScript, Perl, Ruby, MATLABPrejudice
What are the two ways to execute a Python script?
Running in shell mode is suitable for debugging code in a specific part of the script. Using the interpreter, python has the most comprehensive functions and is officially recommended.
What are Pyhton single-line and multi-line annotations used?
Single line comment with "#", multi-line comment with three leading "'''" or four quotation marks """"""
What are the Boolean values?
True or False, True or False
What are precautions for declaring variables?
1. The variable name function contains letters, numbers, and underscores. Variable names can start with letters or underscores, but cannot start with numbers. For example, the agent is incorrect and should be written as agent_1. 2. Variable names cannot contain spaces, but can be separated by underscores. For example, installed apps may cause errors. The correct method is installed_apps. 3. Using the Python keyword to drink a function name cannot be used as a variable, that is, you cannot use Python to retain words for special purposes. 4. Use the lower-case letter l with caution to drink the upper-case letter O, which may be regarded as numbers 1 and 0 by mistake. 5. The variable name should be short and descriptive.Notes
How can I view the address of a variable in the memory?
print(id(x))
3. Code exercises
The user enters the user name and password. If the user name is seven and the password is 123, the logon is successful. Otherwise, the logon fails!
Name = 'seven' ps = 123 user = input ('input Username: ') password = input ('input password:') # print (type (ps ), type (password) if user = name and int (password) = ps: print ('login successfully') else: print ('login failed ')Code
The user can enter the user name and password. If the user name is seven and the password is 123, the logon is successful. Otherwise, the logon fails, and the user can enter the password three times.
Name = 'seven' ps = 123 time = 0 while time <3: user = input ('enter user name: ') password = input ('enter password :') if user = name and int (password) = ps: print ('login successfully') else: print ('login failed') time + = 1Code
The user can enter the user name and password. If the user name is seven or alex and the password is 123, the logon is successful. Otherwise, the logon fails and the user can enter the password three times.
Data = {'seven': {'ps': '000000'}, 'Alex ': {'ps': '000000'} time = 0 while time <3: user = input ('input Username: ') password = input ('input password:') if user in data and password = data [user] ['ps']: print ('login successfully') else: print ('login failed') time + = 1Code
Use the while loop to implement the sum of 2-3 + 4-5 + 6... + 100.
I = 2n = 0 while I <= 100: if I % 2 = 0: n + = I else: n-= I + = 1 print (n)Code while implementation
Use the while loop to output 1, 2, 3, 4, 5, 7, 8, 9, 11, 12; Use the while loop to output all odd numbers in 1-100; use the while loop to output all the even numbers in 1-100.
# While loop fixed number I = 1 while I <13: if I = 6 or I = 10: I + = 1 continue print (I) I + = 1 # while loop odd I = 1 while I <= 100: if I % 2 = 1: print (I) I + = 1 # while loop even I = 1 while I <= 100: if I % 2 = 0: print (I) I + = 1Code
There are two variables below. What is the relationship between n1 and n2?
N1 = 123456
N2 = n1
Print (id (n1), id (n2 ))
Ii. formatting and output exercises
Requirements:
Exercise: Enter the user's Name, age, work, and hobbies and print them in the following format: ------------ info of Egon ----------- Name: EgonAge: 22Sex: maleJob: Teacher ------------- end -----------------Exercise 1
Code:
#!/usr/bin/env python
# coding:utf-8
name=input('your name >>> ')age=input('your age >>> ')sex=input('your sex >>> ')job=input('your job >>> ')print('------------ info of Egon -----------\n' 'name\t: %s\n' 'age \t: %s\n' 'sex \t: %s\n' 'job \t: %s\n' '------------- end -----------------' % (name, age, sex, job))
Effect:
3. while loop exercise 3.1 while loop nesting and other exercises: # It is required that the user's user name and password be verified cyclically as follows, run the command repeatedly. 3. When the command is quit, exit the entire program.Exercise 1
Code:
#!/usr/bin/env python
# coding:utf-8
data={
'zhangsan':{'ps':'zs123'},
'lisi':{'ps':'ls123'}
}
cond=True
while cond:
name=input('your name >>> ')
if not name in data:
print('Incorrect account entry!\n')
continue
passwod=input('your password >>> ')
if passwod==data[name]['ps']:
print('Log in successfully\n')
while cond:
cmd=input('shell # ')
if not cmd:continue
if cmd=='quit':
cond=False
continue
else:
print(cmd)
else:
print('User name or password error!\n')
Effect:
3.2 loop exercises
3.2.1 use the while loop to output 1 2 3 4 5 6 8 9 10.
Num = 1 while num <11: if num = 7: num + = 1 continue print (num) num + = 1Code
3.2.2 calculate the sum of all numbers from 1.
A = 1 I = 0 while a <= 100: I + = a # I = I + a + = 1 print (I)Code
3.2.3 output all odd numbers in 1-100 and all even numbers in 1-100.
# Odd num = 0 while num <101: if num % 2 = 1: print (num) num + = 1 # Even num = 0 while num <101: if num % 2 = 0: print (num) num + = 1Code
3.2.4. Calculate the sum of all numbers of 1-2 + 3-4 + 5... 99.
# While loop num = 0 I = 0 while I <100: if I % 2 = 0: num-= I else: num + = I + = 1 print (num) # For num = 0 for I in range (100): if I % 2 = 0: num = num-I else: num = num + I print (num) ### rslt = 0 for n in range (1,100): rslt + = n * (-1, 1) [n & 1] print (rslt)Code
3.2.5. User Login (three chances to retry ).
Data = {'hangsan ': {'ps': 'zs123'}, 'lisi': {'ps': 'ls123'} time = 0 while time <3: name = input ('your name >>> ') if not name in data: print ('no user \ n ') continue passwd = input ('your password >>> if passwd = data [name] ['ps']: print ('login successful! ') Exit () else: print ('user name or password is incorrect! \ N') time + = 1 else: print ('maximum login times exceeded ')Code
3.2.6 game of guessing age
Requirements:
The user is allowed to try three times at most. If no guess is made for three times, the user will exit directly. If yes, print the congratulations message and exit.
Age = 36 I = 0 while I <3: user_age = input ('Please guess the age> ') # print (type (age), type (user_age )) if user_age = str (age): print ("That's right. that's good! ") Exit () else: print ('Guess wrong! ') I + = 1Code
Upgrade requirements:
Allow users to try up to three times
After three attempts, if you haven't guessed it, you can ask whether you want to continue playing. If you want to answer Y or y, you can continue to let it guess three times, if N or n is answered, exit the program.
Just quit.
Age = 36 I = 0 while I <3: user_age = input ('Please guess the age> ') # print (type (age), type (user_age )) if user_age = str (age): print ("That's right. that's good! ") Exit () else: print ('Guess wrong! ') I + = 1 if I = 3: con = input (' continue? Continue to input "y" ') if con = ('y' or 'y'): I = 0 continue else: print ('stick to it to win, worship ~ ') ContinueCode