从“For-loops"转换到“While-loops" [英] Convert from "For-loops" to "While-loops"

查看:17
本文介绍了从“For-loops"转换到“While-loops"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经解决了这个我正在努力解决的问题.它要求我将代码从for-loops"转换为while-loops":.

def print_names2(people):对于人在人:to_print = ""本人姓名:to_print += 姓名 + " "打印(到_打印)

我只完成了前半部分:

def print_names2(people):我 = 0当我 <len(人[i]):打印(一)我 += 1

当我测试它时:

print_names2([['John', 'Smith'], ['Mary', 'Keyes'], ['Jane', 'Doe']])

返回:

<预><代码>01

有人可以向我澄清如何做,因为我很确定我的答案方法有点远.

解决方案

你又忘记索引people;你只是打印索引.您还想遍历 people 中的 all 条目,而不仅仅是第一个子列表中的名称:

def print_names2(people):我 = 0当我 <伦(人):打印(人[i])我 += 1

这只会循环外部列表.如果要遍历内部子列表,请添加第二个 while 循环:

def print_names2(people):我 = 0当我 <伦(人):j = 0当 j 

所有这些都会直接打印名称,并且所有名称都将以新行结束,而不是将每个子列表打印在一个中间有空格的行上.如果您需要复制字符串构建,请这样做并且在内部 while 循环结束之前不要打印:

def print_names2(people):我 = 0当我 <伦(人):to_print = ""j = 0当 j 

现在最接近带有 for 循环的原始版本.

另一种版本可以创建列表的副本,然后从这些列表中删除项目,直到它们为空:

def print_names2(people):我 = 0当我 <伦(人):人 = 列表(人 [i])to_print = ""而人:名称 = person.pop(0)to_print += 姓名 + " "打印(到_打印)我 += 1

我使用索引离开了外循环.

I've approached this question that I'm struggling to solve. It's asking me to convert the code from "for-loops" to "while-loops":.

def print_names2(people):
    for person in people:
        to_print = ""
        for name in person:
            to_print += name + " "
        print(to_print)

I've only managed to do the first half:

def print_names2(people):
    i = 0        
    while i < len(people[i]):
        print(i)
        i += 1

When I test it with:

print_names2([['John', 'Smith'], ['Mary', 'Keyes'], ['Jane', 'Doe']])

Returns:

0
1

Could someone clarify to me how to do it, as I'm pretty sure my approach to the answer is a bit far away.

解决方案

You are forgetting to index into people again; you are printing just the index. You also want to loop over all entries in people not just the names in the first sub-list:

def print_names2(people):
    i = 0        
    while i < len(people):
        print(people[i])
        i += 1

This only loops over the outer list. If you want to loop over the inner sublists, add a second while loop:

def print_names2(people):
    i = 0        
    while i < len(people):
        j = 0
        while j < len(people[i])
            print(people[i][j])
            j += 1
        i += 1

All this prints the names directly, and all names will end up on new lines rather than each sublist printed on one with a space in between. If you needed to replicate the string building, do so and not print until the inner while loop has ended:

def print_names2(people):
    i = 0        
    while i < len(people):
        to_print = ""
        j = 0
        while j < len(people[i])
            to_print += people[i][j] + " "
            j += 1
        print(to_print)
        i += 1

This now is closest to the original version with the for loops.

An alternative version could create copies of the lists and then remove items from those lists until they are empty:

def print_names2(people):
    i = 0        
    while i < len(people):
        person = list(people[i])
        to_print = ""
        while person:
            name = person.pop(0)
            to_print += name + " "
        print(to_print)
        i += 1

I left the outer loop using an index.

这篇关于从“For-loops"转换到“While-loops"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆