大部分代码是在ipython环境下,所以复制过来时有命令提示符~ 题目所用知识点在题后写出 1、倒序输出10以内数字:for,range() range()表示的范围是前闭后开
In [9]: for i in range(10,0,-1):
...: print (i)
...:
...:
10
9
8
7
6
5
4
3
2
1
2、倒序打印 while
flag=-10
while flag:
flag+=1
print(flag)
-------------
-9
-8
-7
-6
-5
-4
-3
-2
-1
0
3、输出0-1000内能被7整除的数字 for, range() ,break, if ;continue,while,if,break
---------方法1---------------
count=0
for i in range(0,1000,7):
print(i)
count+=1
if count>=20:
break
---------方法2------------
count=0
i=0
while True:
print(i)
i+=7
count+=1
if count>=20:
break
4、打印奇偶数,range(),for,if;continue
-----------方法1-------------
In [11]: for i in range(10):
...: if i % 2:
...: print(i)
...:
1
3
5
7
9
------------方法2------------
In [12]: for i in range(10):
...: if i % 2:
...: print(i)
...: continue
...:
...:
1
3
5
7
9
5、关于continue的用法:
In [17]: for i in range(10):
...: if i % 2 == 1:
...: continue
...: print(i)
...:
...:
...:
...:
...:
0
2
4
6
8
-----------------
In [16]: for i in range(10):
...: if i % 2 == 0:
...: continue
...: print(i)
...:
...:
...:
...:
...:
1
3
5
7
9
6、break用法: 打印20个1-1000内7的倍数
In [20]: for i in range(0,1000,7):
...: print(i)
...: count+=1
...: if count >=20:
...: break
...:
0
7
14
21
28
35
42
49
56
63
70
77
84
91
98
105
112
119
126
133
ipython内查看函数用法举例:
In [17]: help (range)
7、题目如下: 正序输出各位 input,if…else,for
num = int(input("please input a number between 0 and 99999"))
if 0 > num or num > 99999:
print ('wrong input')
elif num >= 1000:
if num >= 10000:
digit=5
else:
digit=4
else:
if num >= 100:
digit=3
elif num >= 10:
digit=2
else:
digit=1
print(digit)
pre=0
print("---------------")
for i in range(digit,0,-1):
cur = num//(10**(i-1))
print(cur-pre*10)
pre=cur
倒序输出各位 while,if,break
num=int(input('please input a number between 0 and 99999'))
digit=0
while True:
print(num%10)
num=num//10
digit+=1
if num == 0:
break
print("你输入的位数是:",digit)
|