习题57、 用户输入不同的数据,当输入的数据达到3个数字的时候,求和结束程序。(数字可以是整数)
提示:判断是否整数的方法,isdigit()
遍历所有的输入数据,判断是否在0-9的字符串范围内
方法1:
#coding=utf-8
result=0
count=0
while True:
s=input("please input the number:")
for v in s:
if v not in "0123456789":#如果不是数字跳出当前循环
break
else:
count+=1
result +=int(s)
if count==3:
break
print(result)
方法2:**利用isdigit()函数
知识点:isdigit() 函数的作用:检测字符串是否只由数字组成。
result=0
count=0
while True:
s=input("please input the number:")
if s.isdigit():
count+=1
result+=int(s)
if count==3:
break
print(result)
习题58:用嵌套列表的方式,遍历输出一个矩阵
方法1: