while.md 3.0 KB
Newer Older
F
feilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
# Python 循环1

使用 while 遍历,打印列表信息

```python
if __name__ == '__main__':
    list = [
        {
            "id": 955350543,
            "number": 2337,
            "title": "Release 1.6 does not have pre-autoreconf'ed configure script",
            "body": "If you have a usage question, please ask us on either Stack Overflow (https://stackoverflow.com/questions/tagged/jq) or in the #jq channel (http://irc.lc/freenode/%23jq/) on Freenode (https://webchat.freenode.net/)."
        },
        {
            "id": 954792209,
            "number": 2336,
            "title": "Fix typo",
            "body": ""
        }
    ]

    # TODO(You): 请在此实现while循环遍历打印代码
```

请选出下列**实现不正确**的代码。

## template

```python

def test(list):
    i = 0
    while i < len(list):
        item = list[i]
        print('')
        print("## 第{}条信息".format(i))
        print("* id: {}".format(item['id']))
        print("* number: {}".format(item['number']))
        print("* title: {}".format(item['title']))
        print("* body: {}".format(item['body']))
        i += 1


if __name__ == '__main__':
    list = [
        {
            "id": 955350543,
            "number": 2337,
            "title": "Release 1.6 does not have pre-autoreconf'ed configure script",
            "body": "If you have a usage question, please ask us on either Stack Overflow (https://stackoverflow.com/questions/tagged/jq) or in the #jq channel (http://irc.lc/freenode/%23jq/) on Freenode (https://webchat.freenode.net/)."
        },
        {
            "id": 954792209,
            "number": 2336,
            "title": "Fix typo",
            "body": ""
        }
    ]
    test(list)
```

## 答案

```python
while i in list:
    item = list[i]
    print('')
    print("## 第{}条信息".format(i))
    print("* id: {}".format(item['id']))
    print("* number: {}".format(item['number']))
    print("* title: {}".format(item['title']))
    print("* body: {}".format(item['body']))
```

## 选项

### A

```python
i = 0
while i < len(list):
    item = list[i]
    print('')
    print("## 第{}条信息".format(i))
    print("* id: {}".format(item['id']))
    print("* number: {}".format(item['number']))
    print("* title: {}".format(item['title']))
    print("* body: {}".format(item['body']))
    i += 1
```

### B

```python
i = 0
while True:
    item = list[i]
    print('')
    print("## 第{}条信息".format(i))
    print("* id: {}".format(item['id']))
    print("* number: {}".format(item['number']))
    print("* title: {}".format(item['title']))
    print("* body: {}".format(item['body']))
    i += 1
    if i==len(list):
        break
```

### C

```python
it = iter(list)
item = next(it, -1)
i = 0
while item != -1:
    print('')
    print("## 第{}条信息".format(i))
    print("* id: {}".format(item['id']))
    print("* number: {}".format(item['number']))
    print("* title: {}".format(item['title']))
    print("* body: {}".format(item['body']))
    item = next(it, -1)
    i += 1
```