1.让用户输入任意字符串,获取字符串之后并计算其中有多少个数字。
total = 0
text = input("请输入内容")
a = 0
while a <len(text):
if text[a].isdigit():
total += 1
a += 1
print(total)
2.实现一个整数加法计算器(两个数相加):(字符串和列表)
思路一:
content = input('请输入:') # [5+9] 或 [5 +9] 或者 [ 5 + 9 ]
result = content.split('+')
# print(result) # ['55 ', ' 99 ']
v1 = int(result[0]) # "55"
v2 = int(result[1]) # " 99 "
v3 = v1 + v2
print(v3)
思路二:
content = input('请输入:') # [5+9] 或 [5 +9] 或者 [ 5 + 9 ]
content = content.strip() # [5+9] 或 [5 +9] 或者 [5 + 9]
v1 = int(content[0])
v2 = int(content[-1])
v3 = v1 + v2
print(v3)
3.录入用户和密码并校验(列表)
users = []
for i in range(0,3)
name = input("请输入用户名和密码")
users.append(name)
print(users) #录入用户和密码 ['alex,123', 'oldboy,888', 'lishaoqi,123']
users_name = input("请输入用户名")
password = input('请输入密码') #用户和密码校验
for items in users:
result = items.split(",")
use = result[0]
pwd = result[1]
if user == username and pwd == password:
print('登入成功')
break
4.请用代码实现:(字典)
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.创建出一个用户列表,然后让用户输入用户名和密码进行登陆,当输入N时,不再继续输入(列表加字典)
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('请输入用户名')
if con == "N":
break
p = input('请输入密码')
info = {}
info['user'] = con
info['pwd'] = p
user_list.append(info)
print(user_list) #构建用户列表
name= input('请输入用户名')
pwd = input('请输入密码')
result = '登入失败'
for i in user_list:
if i['user'] == name and i['pwd'] == pwd:
result = '登入成功'
break
print(result)
6.输出商品列表,用户输入序号,显示用户选中的商品
商品列表:
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998}
]
要求:
1:页面显示 序号 + 商品名称 + 商品价格,如:
1 电脑 1999
2 鼠标 10
...
2:用户输入选择的商品序号,然后打印商品名称及商品价格
3:如果用户输入的商品序号有误,则提示输入有误,并重新输入。
4:用户输入Q或者q,退出程序。
goods = [
{"name": "电脑", "price": 1999},
{"name": "鼠标", "price": 10},
{"name": "游艇", "price": 20},
{"name": "美女", "price": 998}
]
a = 1
for i in goods:
print(a,i)
a += 1
while True:
content = input('请选择商品序号:')
if content.upper() == 'Q':
break
elif int(content) >len(goods) or int(content) <1:
print('输入有误,请重新输入')
continue
else:
print(goods[int(content)-1]['name'],goods[int(content)-1]['price'])
7. 让用户输入任意字符串,然后判断此字符串是否包含指定的敏感字符。
char_list = ['123','456','789']
content = input('请输入内容:')
success = True
for v in char_list:
if v in content:
success = False
break
if success:
print(content)
else:
print('包含铭感字符')