- 1. 写一个简单(+-*/)的计算器, 确保输入的都是数字(如果不是数字,让它重新输入)
- 2. while单层循环完成9 * 9乘法表
- 3. str字符串中的strip, replace, split, partition, expandtabs, join,center, ljust, rjust
- 4. 格式化输出:
输入格式必须是: 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))
结果演示:
i = 1
j = 1
while i < 10:
print(i, "*", j, "=", i*j, end="t")
if i == j:
j = 0
i += 1
print()
j += 1
代码截图:
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填充(默认为空格)至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
姓名 年龄 性别 家庭住址
xxx xxxx
list_data = [{name: xxx, age: xxx, gender: xxx, address}, .....]
包含居中对齐,向左对齐, 向右对齐
list_data = [{'name': 'Roy', 'age': 20, 'gender': '男生', 'address': 'Fujian'}]
print('我的名字是{[name]:*^5}'.format(list_data[0]),
'今年{[age]:*<5}岁'.format(list_data[0]),
'我是{[gender]:*>5}'.format(list_data[0]),
'我住在{[address]}'.format(list_data[0]))
结果截图:



