1. 写一个简单(±*/)的计算器, 确保输入的都是数字(如果不是数字,让它重新输入)
输入格式必须是: 7+8 => 输出格式是 7 + 8 = 15
代码:
#第一种方法
print('输入格式必须是: 7+8 => 输出格式是 7 + 8 = 15')
user_input = input('请按正确格式输入:')
first_num = user_input.rpartition('+')[0]
second_num = user_input.rpartition('+')[2][0]
if first_num.isalpha() or second_num.isalpha():
print('请重新输入')
else:
if user_input.count('+') > 0:
first_num = float(user_input.rpartition('+')[0])
operator = user_input.rpartition('+')[1]
second_num = float(user_input.rpartition('+')[2].rpartition('=')[0])
print('{0}{1}{2}{3}{4: .2f}'.format(first_num, operator, second_num, '=', first_num + second_num))
elif user_input.count('-') > 0:
first_num = float(user_input.rpartition('-')[0])
operator = user_input.rpartition('-')[1]
second_num = float(user_input.rpartition('-')[2].rpartition('=')[0])
print('{0}{1}{2}{3}{4: .2f}'.format(first_num, operator, second_num, '=', first_num - second_num))
elif user_input.count('*') > 0:
first_num = float(user_input.rpartition('*')[0])
operator = user_input.rpartition('*')[1]
second_num = float(user_input.rpartition('*')[2].rpartition('=')[0])
print('{0}{1}{2}{3}{4: .2f}'.format(first_num, operator, second_num, '=', first_num * second_num))
elif user_input.count('/') > 0:
first_num = float(user_input.rpartition('/')[0])
operator = user_input.rpartition('/')[1]
second_num = float(user_input.rpartition('/')[2].rpartition('=')[0])
print('{0}{1}{2}{3}{4: .2f}'.format(first_num, operator, second_num, '=', first_num / second_num))
#第二种方法:
flag = 1代表正常情况
while True:
flag = 1
input_data = input("请输入(输入的格式类似于7+8, 输入exit结束):")
if input_data == "exit":
print("正在退出......")
break
# print(input_data)
# 7 * 8, 7 /8, 7 -8, 7 + 8
result_tuple = ()
operator_list = ["+", "-", "/", "*"]
for operator in operator_list:
if operator in input_data:
result_tuple = input_data.partition(operator)
if result_tuple:
if result_tuple[0].isdigit() and result_tuple[2].isdigit():
num1 = int(result_tuple[0])
num2 = int(result_tuple[2])
if result_tuple[1] == "+":
print(num1, "+", num2, "=", num1 + num2)
elif result_tuple[1] == "-":
print(num1, "-", num2, "=", num1 - num2)
elif result_tuple[1] == "/" and num2 != 0:
print(num1, "/", num2, "=", num1 / num2)
elif result_tuple[1] == "*":
print(num1, "*", num2, "=", num1 * num2)
else:
flag = 0
else:
flag = 0
if flag == 0:
print("输入有误,请重新输入!!!")
截图:
2. while单层循环完成9 * 9乘法表
i = 1
j = 1
while i < 10:
print(i, "*", j, "=", i*j, end="\t")
if i == j:
j = 0
i += 1
print()
j += 1
代码截图:
3. str字符串中的strip, replace, split, partition, expandtabs, join,center, ljust, rjust
1,strip(self, chars=None, /) 该函数的作用是去除字符串开头和结尾处指定的字符,不会去除字符串中间对应的字符
2, replace(self, old, new, count=-1, /) 把str.中的 old 替换成 new,如果 count 指定,则替换不超过 count次.。
3,split(self, /, sep=None, maxsplit=-1) 拆分字符串。通过指定分隔符sep对字符串进行分割,并返回分割后的字符串列表。
4,partition(self, sep, /) 根据指定的分隔符(sep)将字符串进行分割。从字符串左边开始索引分隔符sep,索引到则停止索引。
5,expandtabs(self, /, tabsize=8) 将字符串S中的 \t 替换为一定数量的空格。默认N=8
6,join(self, iterable, /) 将iterable变量的每一个元素后增加一个str字符串。
7,center(self, width, fillchar=’ ‘, /) 返回一个居中的新字符串,可以自行设置宽度和填充字符
8,ljust(self, width, fillchar=’ ‘, /) 返回一个原字符串左对齐,并使用fillchar填充(默认为空格)至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
9.rjust(self, width, fillchar=’ ', /) 返回一个原字符串右对齐,并使用fillchar填充(默认为空格)至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
4. 格式化输出:
姓名 年龄 性别 家庭住址
xxx xxxx
list_data = [{name: xxx, age: xxx, gender: xxx, address}, …]
包含居中对齐,向左对齐, 向右对齐
list_data = [
{"name": "alex", "age": 18, "gender": "male", "address": "xxxxxxxxxxxxxx"},
{"name": "tony", "age": 18, "gender": "female", "address": "xxxxxxxxxxxxxx"},
{"name": "john", "age": 18, "gender": "male", "address": "xxxxxxxxxxxxxx"}
]
print(f"{'name':^10}{'age':^8}{'gender':^10}{'address':^60}")
print("{0[name]:^10}{0[age]:^8}{0[gender]:^10}{0[address]:^60}".format(list_data[0]))
print("{name:^10}{age:^8}{gender:^10}{address:^60}".format_map(list_data[1]))
print(f"{list_data[2]['name']:^10}{list_data[2]['age']:^8}{list_data[2]['gender']:^10}{list_data[2]['address']:^60}")
结果截图: