1. Let the user enter any string, get the string and calculate how many digits there are.
total = 0 text = input("Please enter the content") a = 0 while a <len(text): if text[a].isdigit(): total += 1 a += 1 print(total)
2. Implementing an integer addition calculator (adding two numbers): (strings and lists)
Train of thought:
content = input('Please input:') # [5+9] or [5 +9] perhaps [ 5 + 9 ] result = content.split('+') # print(result) # ['55 ', ' 99 '] v1 = int(result[0]) # "55" v2 = int(result[1]) # " 99 " v3 = v1 + v2 print(v3)Train of thought two:content = input('Please input:') # [5+9] or [5 +9] perhaps [ 5 + 9 ]
content = content.strip() # [5+9] or [5+9] or [5+9]
v1 = int(content[0])
v2 = int(content[-1])
v3 = v1 + v2print(v3)
3. Enter user and password and check (list)
users = [] for i in range(0,3) name = input("Please enter a username and password") users.append(name) print(users) #Enter user and password ['alex,123', 'oldboy,888', 'lishaoqi,123'] users_name = input("enter one user name") password = input('Please input a password') #User and password verification for items in users: result = items.split(",") use = result[0] pwd = result[1] if user == username and pwd == password: print('Login successfully') break
4. Please implement it in code: (dictionary)
message = "k1|v1,k2|v2,k3|123......." # ,k3|123
info = {'k1':'v1','k2':'v2','k3':'123'}
info = {} message = "k1|v1,k2|v2,k3|123" for item in message.split(','): # ["k1|v1","k2|v2","k3|123"] v1,v2 = item.split('|') info[v1] = v2 print(info)
5. Create a user list, and then let the user enter a username and password for login. When entering N, no more input (list plus dictionary)
user_list = [
{'user':'alex','pwd':'123'},
{'user':'oldboy','pwd':'123'},
{'user':'lishaoqi','pwd':'1123'},
{'user':'liqihang','pwd':'123'},
{'user':'xxx','pwd':'123'}, # N
]
user_list = [] while True: con = input('enter one user name') if con == "N": break p = input('Please input a password') info = {} info['user'] = con info['pwd'] = p user_list.append(info) print(user_list) #Building User Lists name= input('enter one user name') pwd = input('Please input a password') result = 'Login failure' for i in user_list: if i['user'] == name and i['pwd'] == pwd: result = 'Login successfully' break print(result)
6. Output commodity list, user input serial number, display the selected commodity
List of goods:
goods = [
{"name": "computer", "price": 1999},
{"name": "mouse", "price": 10},
{"name": "yacht", "price": 20},
{"name": "beauty", "price": 998}
]
Requirement:
1: The page shows serial number + commodity name + commodity price, such as:
1 Computer 1999
2 mouse 10
...
2: The user enters the serial number of the selected commodity, and then prints the name and price of the commodity.
3: If the serial number of the goods entered by the user is incorrect, the user will be prompted for incorrect input and re-input.
4: The user enters Q or Q and exits the program.
goods = [ {"name": "Computer", "price": 1999}, {"name": "mouse", "price": 10}, {"name": "Yacht", "price": 20}, {"name": "Beauty", "price": 998} ] a = 1 for i in goods: print(a,i) a += 1 while True: content = input('Please choose the serial number of the goods.:') if content.upper() == 'Q': break elif int(content) >len(goods) or int(content) <1: print('Incorrect input,Please re-enter') continue else: print(goods[int(content)-1]['name'],goods[int(content)-1]['price'])