1.统计不同字符个数。
用户从键盘输入一行字符,统计并输出其中英文字符、数字、空格和其他字符的个数。
str=input("请输入一串字符:")
english=0
num=0
space=0
other=0
for i in str:
#ord调用ASCII码数值
if(ord(i)>=97 and ord(i)<=122)or(ord(i)>=65 and ord(i)<=90):
english=english+1
elif(ord(i)>=48 and ord(i)<=57):
num=num+1
elif(ord(i)==32):
space=space+1
else:
other=other+1
print('英文字符有:',english,'数字有:',num,'空格有:',space,'其他字符有:',other)
2.猜拳游戏,打印出猜拳结果和输赢总次数
import random #随机数模块
win=0
lose=0
tie=0
while True:
print('='*30)
print('*'*10,'欢迎来猜拳','*'*10)
print('历史成绩 赢:%s 输:%s 平:%s'%(win,lose,tie))
print('1.石头 2.剪刀 3.布 4.退出')
a=random.choice(['石头','剪刀','布'])
b=input('请输入:')
if (a=='石头' and b=='2')or(a=='剪刀' and b=='3')or(a=='布' and b=='1'):
lose+=1
print ('你输了')
elif (a=='石头' and b=='3')or(a=='剪刀' and b=='1')or(a=='布' and b=='2'):
win+=1
print ('你赢了')
elif (a=='石头' and b=='1')or(a=='剪刀' and b=='2')or(a=='布' and b=='3'):
tie+=1
print('平局')
elif(b=='4'):
print('退出')
break
else:
print('输入错误,请重新输入')