美文网首页
《笨办法学Python3》练习三十三:while循环

《笨办法学Python3》练习三十三:while循环

作者: 雨开Ame | 来源:发表于2019-03-06 17:42 被阅读0次

练习代码

i = 0
numbers = []

while i < 6:
    print(f"At the top i is {i}")
    numbers.append(i)

    i = i + 1
    print("Numbers now: ", numbers)
    print(f"At the bottom i is {i}")

print("The numbers: ")

for num in numbers:
    print(num)

Study Drills

  1. Convert this while-loop to a function that you can call, and replace 6 in the test (i < 6) with a variable.
i = 0
numbers = []

def loop_func(end, i, numbers):
    while i < end:
        print(f"At the top i is {i}")
        numbers.append(i)

        i += step
        print("Numbers now: ", numbers)
        print(f"At the bottom i is {i}")        

loop_func(6, i, numbers)

print("The numbers: ")

for num in numbers:
    print(num)
  1. Use this function to rewrite the script to try different numbers.

  2. Add another variable to the function arguments that you can pass in that lets you change the + 1 on line 8 so you can change how much it increments by

i = 0
numbers = []

def loop_func(end, step, i, numbers):
    while i < end:
        print(f"At the top i is {i}")
        numbers.append(i)

        i = i + 1
        print("Numbers now: ", numbers)
        print(f"At the bottom i is {i}")        

loop_func(6, 2, i, numbers)

print("The numbers: ")

for num in numbers:
    print(num)
  1. Rewrite the script again to use this function to see what effect that has.

  2. Write it to use for-loops and range. Do you need the incrementor in the middle anymore?
    What happens if you do not get rid of it?

设置一个结束值

i = 0
numbers = []
# 设置一个结束值
end = 6

for i in range(6):
  print(f"At the top i is {i}")
  numbers.append(i)
  print("Numbers now: ", numbers)
  print(f"At the bottom i is {i}")

print("The numbers: ")

for num in numbers:
  print(num)

相关文章

  • 《笨办法学Python3》练习三十三:while循环

    练习代码 Study Drills Convert this while-loop to a function t...

  • 学习Python_练习:1.乘法表

    使用Python3编写乘法表的练习,使用 for 和 while 两种循环 效果: 代码:

  • C 语言循环变量(2)

    do while 循环 for 循环 练习 do while循环 谨记先循环,后判断 练习 答案D 讲解while...

  • 2018-10-30

    Python3 while循环 0基础的我最近在学python,看到while循环这个章节时,初看很简单,但自己在...

  • Python基础 | 第二课:循环、字符串、列表

    Python3笔记 | 第二课:循环、字符串、列表 程序三大执行流程 while循环 while 判断条件 :条件...

  • 04 C循环结构

    1、while循环 1.1、 whlie小练习 2、for循环

  • 08 循环语句

    for循环 while循环 do-while循环 三种循环的区别 练习 break关键字 continue关键字 死循环

  • 2018-05-29

    循环结构 1.while(条件){//到条件为真时执行的命令} while结束循环时用break; 练习:...

  • python3-基础篇-常用操作

    整理了一些常用的python3基础操作: 包括:if判断、逻辑运算、while循环、for循环、类型转换、列表增删...

  • 3.循环结构

    while循环结构 while(循环条件){循环操作} 练习题 老师每天检查张三的学习任务是否合格,如果不合格,则...

网友评论

      本文标题:《笨办法学Python3》练习三十三:while循环

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