Python basic exercises

1. Number of printing 1-100 for i in range(1, 101): print(i) 2. Print the sum of 1 + 2 +.. 100 num = 1 sum = 0 while num < 101: sum += num num += 1...
1. Number of printing 1-100
2. Print the sum of 1 + 2 +.. 100
3. Use while to output 1 2 3 4 6
4. Output all odd numbers in 1-100
5. User authentication login
6. Format output
7. Basic operation of python
8. Calculation 1-2 + 3-4... + 99
9. Set user input content detection
10. Simple verification of verification code
11. Add a list user
12. List [11, 22... 99]; those greater than 66 are added to key2, and those less than 66 are added to key1
13. Output the list of commodities, input the serial number, and display the commodities selected by the user
14. Account registration and login verification

1. Number of printing 1-100

for i in range(1, 101): print(i)

2. Print the sum of 1 + 2 +.. 100

num = 1 sum = 0 while num < 101: sum += num num += 1 print(sum)

3. Use while to output 1 2 3 4 6

i = 1 while i < 7: if i == 5: print('') else: print(i) i += 1

4. Output all odd numbers in 1-100

for i in range(1, 101): if i % 2 != 0: print(i)

5. User authentication login

username = 'liangxiao' password = '123..com' username_in = input('Please output your name:') password_in = input('Please output your password:') num = 1 while num < 4: num += 1 if username_in == username and password_in == password: print('Login succeeded! Welcome back!') break else: print('Account and password do not match, please confirm and re-enter!') num = 4 - num Num = 'You still have input times left%d' % (num) print(Num) username_in = input('Please output your name:') password_in = input('Please output your password:')

6. Format output

name = input('Please output your name: ') age = input('Please output your age: ') information = ''' Your personal information is as follows: Your name:% s Your age:% s ''' % (name, age) print(information)

7. Basic operation of python

6 or 2 > 1 = 6 3 or 2 > 1 = 3 0 or 5 < 4 = False 5 < 4 or 3 = 3 2 > 1 or 6 = True 3 and 2 > 1 = True 0 and 3 > 1 = False 2 > 1 and 3 = 3 3 > 1 and 0 = 0 1 > 2 or 4 < 7 and 8 == 8 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2 = 2 not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6 = False

8. Calculation 1-2 + 3-4... + 99

num = 0 sum = 0 while num < 100: num += 1 if num == 88: continue elif num % 2 != 0: sum += num else: sum -= num print(sum)

9. Set user input content detection

input1 = input('Please output the password you need to register:') sensitive_words = ['Flour hammer', 'Big hammer'] i = 0 while i < 3: if input1 in sensitive_words: print('Your output is wrong, please input again!') input1 = input('Please output the password you need to register:') else: print('The output is correct and the login is successful!') print('Welcome back!') break i += 1

10. Simple verification of verification code

code = 'AC8E' code_input = input('Please output the verification code:') if code_input.upper() == code: print('Verifying and passing') else: print('Validation failed')

11. Add a list user

li = [] username = input('>>>Please output the users you need to add:') while True: if username.upper() != 'Q': li.append(username) print('Add success') operation = input('Do you want to continue? ( Y/N)') if operation.upper() == 'Y': username = input('>>>Please output the users you need to add:') elif operation.upper() == 'N': print('welcome to use this program!') break else: print('Your input is wrong!') username = input('>>>Please output the users you need to add:') else: print('welcome to use this program!') break

12. List [11, 22... 99]; those greater than 66 are added to key2, and those less than 66 are added to key1

list1 = [11, 22, 33, 44, 55, 66, 77, 88, 99] dict1 = {'key1':'', 'key2':''} list2 = [] list3 = [] for i in list1: if i > 66: list2.append(i) else: list3.append(i) dict1['key2'] = list2 dict1['key1'] = list3 print(dict1)

13. Output the list of commodities, input the serial number, and display the commodities selected by the user

Product list1 = ["mobile phone", "computer", "mouse pad", "yacht", the requirements are as follows:
1: the page displays the serial number + commodity name, such as:
1 mobile phone
2 computer
3 mouse pad
4 yacht
2: enter the serial number of the selected product and print the product name
3: if the serial number of the product entered by the user is wrong, you will be prompted to enter it again.
4: the user enters Q or Q to exit the program. ""
list1 = ['Mobile phone', 'Computer', 'Mouse pad', 'Yacht'] for i in list1: print('{}\t{}'.format(list1.index(i)+1,i)) while 1: choose = input('Please output your choice:(1-4)') if choose.isdigit(): if int(choose) > 0 and int(choose) < 5: print('The input is correct!') print(list1[int(choose)-1]) break else: print('The sequence you entered is wrong, please input again!') continue elif choose.isalpha(): if choose.upper() == 'Q': print('good-bye!') break else: print('Exit failed, your output is wrong') continue else: print('Your input is wrong, please input again!')

14. Account registration and login verification

while 1: username = input('Please output the account you need to register:') password = input('Please output the password you need to register:') if username.isalnum() and password.isalnum(): print('Can register√') username_new = input('Please reconfirm the account you need to register:') password_new = input('Please confirm the password you need to register again:') if username_new == username and password_new == password: print('Registration succeeded!') break else: input('The registration information is wrong, please re-enter\n') username_file = open('Username', mode='a', encoding='utf-8') password_file = open('Password', mode='a', encoding='utf-8') username_file.write(username) password_file.write(password) username_file.close() password_file.close() for i in range(3): login_username = input('Please login user name:') login_password = input('Please login password:') username = open('Username', mode='r', encoding='utf-8') password = open('Password', mode='r', encoding='utf-8') read_user = username.read() read_pass = password.read() if login_username == read_user and login_password == read_pass: print('Login succeeded!') break else: ii = i + 1 num = 3 - ii print('Login failed, please login again! You still have the rest.{}frequency'.format(num)) else: print('Account locked!')

4 December 2019, 00:08 | Views: 7863

Add new comment

For adding a comment, please log in
or create account

0 comments