熟悉循环结构while与for
row = 1
while row <= 9: # 行
col = 1
while col <= row: # 列
print(str(row) + "X" + str(col) + "=" + str(row * col) + " ", end="") # 输出一行内容
col += 1
print() 换行
row += 1
1X1=1
2X1=2 2X2=4
3X1=3 3X2=6 3X3=9
4X1=4 4X2=8 4X3=12 4X4=16
5X1=5 5X2=10 5X3=15 5X4=20 5X5=25
6X1=6 6X2=12 6X3=18 6X4=24 6X5=30 6X6=36
7X1=7 7X2=14 7X3=21 7X4=28 7X5=35 7X6=42 7X7=49
8X1=8 8X2=16 8X3=24 8X4=32 8X5=40 8X6=48 8X7=56 8X8=64
9X1=9 9X2=18 9X3=27 9X4=36 9X5=45 9X6=54 9X7=63 9X8=72 9X9=81
# 斐波那契数列
# 变量的连续赋值:多个变量存储相同的数据
prev = next = 1
# 获取第n位数据
pos_n = input("请输入想要查询的位置")
pos_n = int(pos_n)
# 循环
index = 1
while index <= pos_n:
if index == 1 or index == 2:
print(1)
else:
prev, next = next, prev + next
#打印当前位置全部数字
print(next)
index += 1
print("查询的位置数字为:",next)
请输入想要查询的位置6
1
1
2
3
5
8
查询的位置数字为: 8
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/114569.html原文链接:https://javaforall.cn