如果循环次数已经确定, for循环
"""
eg05 - 循环结构 --> for-in循环
Author: mimo_yy
Date: 2024/5/15
"""
for i in range(100):
print(i, 'hello world!')
print(i, 'bye~')
for i in range(1, 101):
print(i, 'hello world!')
print(i, 'bye~')
for i in range(1, 101, 2):
print(i, 'hello world!')
print(i, 'bye~')
for i in range(100, 0, -2):
print(i, 'hello world!')
print(i, 'bye~')
print('wow!')
"""
eg06 - 实现0-100的求和
Author: mimo_yy
Date: 2024/5/15
"""
total = 0
for i in range(1, 101):
total = total + i
print(total)
total = 0
for i in range(1, 101, 2):
total = total + i
print(total)
total = 0
for i in range(2, 101, 2):
total = total + i
print(total)
total = 0
for i in range(1, 101):
if i % 2 == 0:
total = total + i
else:
pass
print(total)
total = sum(range(1, 101))
"""
eg07 - 求1-100之间3或5的倍数的和
Author: mimo_yy
Date: 2024/5/15
"""
total = 0
for i in range(1, 101):
if i % 3 == 0 or i % 5 == 0:
total += i
else:
pass
print(total)
"""
eg08 - 输出1-N以及他们对应的平方/立方/N的N次方
1 1 1 1
2 4 8 4
3 9 27 27
4 16 256
Author: mimo_yy
Date: 2024/5/15
"""
ping = 0
li = 0
N = 0
for n in range(1, 5):
ping = n ** 2
li = n ** 3
N = n ** n
print(n, end=' ')
print(ping, end=' ')
print(li, end=' ')
print(N)
print('N'.ljust(10), end='')
print('N^2'.ljust(10), end='')
print('N^3'.ljust(10))
for i in range(1,11):
print(f'{i:<10d}{i**2:<10d}{i**3:<10d}')
"""
eg09 - 输入一个非负整数N,计算N!
N! = N * (N-1)*(N-2)*...* 2 * 1
Author: mimo_yy
Date: 2024/5/15
"""
n = int(input('n= '))
total = 1
for i in range(n, 0, -1):
total *= i
print(f'{n}! = {total}')
n = int(input('n= '))
total = 1
for i in range(2, n + 1):
total *= i
print(f'{n}! = {total}')
"""
eg10 - 输入两个正整数,找出他们的最大公约数
bug --> 第一次出现程序bug是因为一只蛾子飞到晶体管,导致出现问题;所以之后用bug表示问题/故障/缺陷
debug -->解决bug(调试)
Author: mimo_yy
Date: 2024/5/15
"""
x = int(input('x= '))
y = int(input('y= '))
for i in range(x, 0, -1):
if x % i == 0 and y % i == 0:
print(i)
break
x = int(input('x= '))
y = int(input('y= '))
while y % x != 0:
x, y = y % x, x
print(x)