1. 统计字符串(Python3)
题目:输入一个字符,分别编译统计出其中英文字母、空格、数字和其他字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为'\n'
isdigit() 判断是否是数字
isalpha() 判断是否是字母
isalnum() 判断是否是字母和数字组合
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time: 2018-01-24 11:40
# @Author: Feng Xiaoqing
# @File: string.py
status = 1
while status:
string = input("please input a string('quit'will exit): ")
if string == 'quit':
exit(1)
disgit = pha = space = other = 0
for i in string:
if i.isdigit():
disgit += 1
elif i.isalpha():
pha += 1
elif i.isspace():
space += 1
else:
other += 1
print("数字:{0},字母:{1},空格:{2},其他:{3}".format(disgit,pha,space,other))
2.乘法口诀(Python3)
乘法口诀:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time: 2018-01-24 11:57
# @Author: Feng Xiaoqing
# @File: chengfakoujue.py
for i in range(1,10): #行
for j in range(1,i+1):
chengJi = i*j
print("{0} x {1} = {2} ".format(j,i,chengJi),end='')
print()
或:
for i in range(1,10):
for j in range(1,i+1):
chengJi = i*j
print("{0} x {1} = {2} ".format(j,i,chengJi),end='')
if i == j:
print()