鱼C论坛

 找回密码
 立即注册
查看: 1314|回复: 1

[已解决]举一反三不会

[复制链接]
发表于 2021-11-14 12:14:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
怎么用for代替while输出完全相同的内容(包括格式)
  1. ten_things = "Apples Oranges Crows Telephone Light Sugar"

  2. print("Wait there are not 10 things in that list.Let's fix that.")

  3. stuff = ten_things.split(' ')
  4. more_stuff = ["Day", "Night", "Song", "Frisbee",
  5.                 "Corn", "Banana", "Gril", "Boy"]


  6. while len(stuff) != 10:
  7.     next_one = more_stuff.pop()
  8.     print("Adding:", next_one)
  9.     stuff.append(next_one)
  10.     print(f"There are {len(stuff)} items now.")

  11. print("There we go: ", stuff)

  12. print("Let's do some things with stuff.")

  13. print(stuff[1])
  14. print(stuff[-5])# whoa! fancy
  15. print(stuff.pop())
  16. print(' '.join(stuff)) # what? cool!
  17. print('#'.join(stuff[3:5])) # super stellar!
复制代码
最佳答案
2021-11-14 12:56:46
  1. ten_things = "Apples Oranges Crows Telephone Light Sugar"

  2. print("Wait there are not 10 things in that list.Let's fix that.")

  3. stuff = ten_things.split(' ')
  4. more_stuff = ["Day", "Night", "Song", "Frisbee",
  5.                 "Corn", "Banana", "Gril", "Boy"]

  6. n = 10 - len(stuff)
  7. for _ in range(n):
  8.     next_one = more_stuff.pop()
  9.     print("Adding:", next_one)
  10.     stuff.append(next_one)
  11.     print(f"There are {len(stuff)} items now.")

  12. print("There we go: ", stuff)

  13. print("Let's do some things with stuff.")

  14. print(stuff[1])
  15. print(stuff[-5])# whoa! fancy
  16. print(stuff.pop())
  17. print(' '.join(stuff)) # what? cool!
  18. print('#'.join(stuff[3:5])) # super stellar!
复制代码

        运行实况:
  1. D:\00.Excise\Python>python x.py
  2. Wait there are not 10 things in that list.Let's fix that.
  3. Adding: Boy
  4. There are 7 items now.
  5. Adding: Gril
  6. There are 8 items now.
  7. Adding: Banana
  8. There are 9 items now.
  9. Adding: Corn
  10. There are 10 items now.
  11. There we go:  ['Apples', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar', 'Boy
  12. ', 'Gril', 'Banana', 'Corn']
  13. Let's do some things with stuff.
  14. Oranges
  15. Sugar
  16. Corn
  17. Apples Oranges Crows Telephone Light Sugar Boy Gril Banana
  18. Telephone#Light

  19. D:\00.Excise\Python>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-11-14 12:56:46 | 显示全部楼层    本楼为最佳答案   
  1. ten_things = "Apples Oranges Crows Telephone Light Sugar"

  2. print("Wait there are not 10 things in that list.Let's fix that.")

  3. stuff = ten_things.split(' ')
  4. more_stuff = ["Day", "Night", "Song", "Frisbee",
  5.                 "Corn", "Banana", "Gril", "Boy"]

  6. n = 10 - len(stuff)
  7. for _ in range(n):
  8.     next_one = more_stuff.pop()
  9.     print("Adding:", next_one)
  10.     stuff.append(next_one)
  11.     print(f"There are {len(stuff)} items now.")

  12. print("There we go: ", stuff)

  13. print("Let's do some things with stuff.")

  14. print(stuff[1])
  15. print(stuff[-5])# whoa! fancy
  16. print(stuff.pop())
  17. print(' '.join(stuff)) # what? cool!
  18. print('#'.join(stuff[3:5])) # super stellar!
复制代码

        运行实况:
  1. D:\00.Excise\Python>python x.py
  2. Wait there are not 10 things in that list.Let's fix that.
  3. Adding: Boy
  4. There are 7 items now.
  5. Adding: Gril
  6. There are 8 items now.
  7. Adding: Banana
  8. There are 9 items now.
  9. Adding: Corn
  10. There are 10 items now.
  11. There we go:  ['Apples', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar', 'Boy
  12. ', 'Gril', 'Banana', 'Corn']
  13. Let's do some things with stuff.
  14. Oranges
  15. Sugar
  16. Corn
  17. Apples Oranges Crows Telephone Light Sugar Boy Gril Banana
  18. Telephone#Light

  19. D:\00.Excise\Python>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 12:00

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表