鱼C论坛

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

[已解决]举一反三不会

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

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

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

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

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

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


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

print("There we go: ", stuff)

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

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

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

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

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

print("There we go: ", stuff)

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

print(stuff[1])
print(stuff[-5])# whoa! fancy
print(stuff.pop())
print(' '.join(stuff)) # what? cool!
print('#'.join(stuff[3:5])) # super stellar!
        运行实况:
D:\00.Excise\Python>python x.py
Wait there are not 10 things in that list.Let's fix that.
Adding: Boy
There are 7 items now.
Adding: Gril
There are 8 items now.
Adding: Banana
There are 9 items now.
Adding: Corn
There are 10 items now.
There we go:  ['Apples', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar', 'Boy
', 'Gril', 'Banana', 'Corn']
Let's do some things with stuff.
Oranges
Sugar
Corn
Apples Oranges Crows Telephone Light Sugar Boy Gril Banana
Telephone#Light

D:\00.Excise\Python>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

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

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

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

print("There we go: ", stuff)

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

print(stuff[1])
print(stuff[-5])# whoa! fancy
print(stuff.pop())
print(' '.join(stuff)) # what? cool!
print('#'.join(stuff[3:5])) # super stellar!
        运行实况:
D:\00.Excise\Python>python x.py
Wait there are not 10 things in that list.Let's fix that.
Adding: Boy
There are 7 items now.
Adding: Gril
There are 8 items now.
Adding: Banana
There are 9 items now.
Adding: Corn
There are 10 items now.
There we go:  ['Apples', 'Oranges', 'Crows', 'Telephone', 'Light', 'Sugar', 'Boy
', 'Gril', 'Banana', 'Corn']
Let's do some things with stuff.
Oranges
Sugar
Corn
Apples Oranges Crows Telephone Light Sugar Boy Gril Banana
Telephone#Light

D:\00.Excise\Python>
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-1-12 20:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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