对于python的10个小练习做下笔记
1.使用while循环输入1 2 3 4 5 6 8 9 10。。。自己写的代码如下:
num1 = 0
while num1 < 10:
num1 = num1 +1
if num1 == 7 :
continue
print(num1)
视频教程:
n = 1
while n < 11:
if n == 7:
pass
else:
print(n)
n = n +1
2.求1-100的所有数的和
num2 = 1
result = 0
while num2 <= 100:
print(num2)
result = result + num2
num2 = num2+1
print(result)
视频教程:
n = 1
s = 0
while n < 101:
s = s +n
n = n+1
print(s)
3.输出1-100内的所有的奇数
num3 = 1
while num3 <= 100:
if num3 % 2 != 0:
print(num3)
num3 = num3 + 1
视频教程:
NUM1 = 1
while NUM1< 101:
temp = NUM1 % 2
if temp == 0:
pass
else:
print(NUM1)
NUM1 = NUM1 +1
4.输出1 -100 内的所有偶数
num4 = 1
while num4<=100:
if num4 % 2 == 0:
print(num4)
num4 = num4 +1
视频教程:
n = 1
while n < 101:
temp = n % 2
if temp ==0 :
print(n)
else:
pass
n = n+1
print("END----------------")
5.求1-2+3-4+5...99的所有数的和
num5 = 1
result1 = 0
result2 = 0
while num5 < 100:
if num5 % 2!=0:
result1 = result1 + num5
else:
result1 = result1 - num5
num5 =num5 +1
print(result1)
视频教程:
n = 1
s = 0
while n <100:
temp = n % 2
if temp == 0:
s=s-n
else:
s = s+n
print(s)
6.要求用户登录系统,只有3次机会
n = 3
n1 = 'xiaoming'
n2 = ''
while n > 0:
name = input("请输入用户名:")
Pass = input("请输入密码:")
n = n-1
if name == n1 and Pass ==n2:
print("欢迎来到东京热!")
break
elif name == n1 and Pass !=n2:
print("您输入的密码错误,请重新输入,您还有%d次机会"%n)
else:
print("请输入正确的用户名!您还有%d次机会"%n)
7.打印空心倒三角形
line=5
for i in range(1,line+1):
for y in range(i):
print(' ',end='')
if i ==1 or i == 5:
for z in range(2*line-1):
print('*',end='')
else:
for z in range(2 * line - 1):
if z ==0 or z==2*line -2:
print('*',end='')
else:
print(' ',end='')
print()
line = line - 1
8.打印正空心三角形
line=5
for i in range(1,line+1):
for y in range(line - i):
print(' ',end = '')
if i ==1 or i == line:
for z in range(i*2-1):
print('*',end='')
else:
for z in range(i*2-1):
if z ==0 or z==i*2-2:
print('*',end = '')
else:
print(' ',end = '')
print()
9.打印菱形实心的
max_level = 5
for i in range(1,max_level):
for y in range(max_level-i):
print(' ',end = '')
for z in range(2*i-1):
print('*',end = '')
print()
for i in range(1,max_level+1):
for z in range(i-1):
print(' ',end='')
for y in range(2*max_level-1):
print('*',end='') max_level -= 1
print()
10.打印出空心的菱形
line=5
for i in range(1,line):
for y in range(line+1 - i):
print(' ',end = '')
if i ==1 :
for z in range(i*2-1):
print('*',end='')
else:
for z in range(i*2-1):
if z ==0 or z==i*2-2:
print('*',end = '')
else:
print(' ',end = '')
print()
for i in range(1,line+1):
for y in range(i):
print(' ',end='')
if i == 5:
for z in range(2*line-1):
print('*',end='')
else:
for z in range(2 * line - 1):
if z ==0 or z==2*line -2:
print('*',end='')
else:
print(' ',end='')
print()
line = line - 1