python学习笔记day04 昨天作业

实现一个简单计算器

当用户输入:2+3 ,4+ 5 等可以直接计算出结果

content=input("please input content:")
numbers=content.split('+')
sum=0
for number in numbers:
    sum=sum+int(number)
print(sum)

其实可以另一种方法来做(考虑到用户输入的极端情况,就是中间有很多空格)

#仅限于两个数相加
content=input("please input numbers:")
flag=content.find('+')  #可以找到+所在的位置
a=int(content[:flag])
b=int(content[flag+1:])
print(a+b)

用户输入字符和数字,统计输入内容中数字出现的次数

很明显,需要做两步:

判断数字: isdigit()

是数字则累加;

msg=input("please input some message:")
count=0  #统计数字出现的次数
for s in msg:
    if s.isdigit():
        count+=1
print(count)
talk is cheap,show me the code
原文地址:https://www.cnblogs.com/xuanxuanlove/p/9461617.html