美文网首页
python 3个人学习笔记1-while

python 3个人学习笔记1-while

作者: 色拉丼 | 来源:发表于2017-11-01 12:09 被阅读0次

同样是循环1*1,1*2,……9*9,用for可以三行完事,如果用while就会麻烦一些但还是可以实现的。


for num in range(1,10):

    for num1 in range(1,10):

        print(str(num)+"*"+str(num1)+"="+str(num*num1))

第一行:循环1-9(要注意的是并非1-10)

第二行:循环每一个数字时加入第二个数字并循环至9

第三行:打印结果

是个人都看得懂

然而到了while时:


num=1

num1=1

while num <= 3:

        while num1 <= 3:

            print(num1*num)

            num1 += 1

    num += 1

    num1=1

前7行基本和for循环一致,但是因为在这里1-9是手动一个一个计算的,所以当第二个while并非自动重来,而是需要在第一个循环里(第二个循环外)手动变回1重新循环。

相关文章

网友评论

      本文标题:python 3个人学习笔记1-while

      本文链接:https://www.haomeiwen.com/subject/lcgwpxtx.html