使用 while 循环打印输出的 Python 作业

标签 python python-3.x while-loop

我正在尝试学习“while”循环和计数器。我了解如何在基本级别上使用它们,但我觉得在这种情况下我已经过度使用它们了,可能有更好的初学者答案仍然使用 while 循环和 if/elif/else 语句。

基本上,程序应该根据从 0 开始的计数器打印一首诗中的每个句子并打印句子 1,然后在第 4 句之后打印副歌...然后继续打印下 4 个句子,然后打印副歌最后两次。

这就是我现在所处的位置,但就像我提到的那样,我觉得我已经结束了使用 while 循环来使它更简单,而且感觉有点像作弊。

ver1 = ['sentence1', 'sentence2', 'sentence3', 'sentence4']
ver2 = ['sentence5', 'sentence6', 'sentence7', 'sentence8']
chor1= ['chorus1', 'chorus2']

counter = 0

while counter == 0:
    print(ver1[0])
    counter += 1

while counter == 1:
    print(ver1[1])
    counter += 1

while counter == 2:
    print(ver1[2])
    counter += 1

while counter == 3:
    print(ver1[3])
    counter += 1

#this if statement was my decision just to see if I could use it properly, but I'd like to do the entire thing with while loops if possible....but using if/elif/else statements isn't forbidden.     
if counter >= 5:
    print(ver1[3])
else:
    print(chor1[0])

我一开始用 if 语句创建它,但老师问我是否会尝试用 while 循环来做它作为家庭作业...这是我写的原始 if/elif/else 语句。

verse1 = "I came home and my dog was gone"
verse2 = "She took my dog and my truck"
verse3 = "oh no she didn't"
chorus = "ohh how times have changed"

truck = 'gone'
dog = 'gone'

if dog == "gone":
    print (verse1)
    print (chorus)
else:
    print(verse3)

if truck == 'gone':
    print (verse2)
    print (chorus)
else:
    print (verse3)

这只是程序的第一部分,因为如果有比复制/粘贴更好的答案,我不想继续它并浪费我的时间,而循环进行一些小的编辑以确定要打印的内容。

最佳答案

This is where I'm at now, but like I mentioned I feel like I'm over using the while loops making it simpler and it kind of feels like cheating.

实际上,您过度使用了 while 循环,使它变得更复杂

让我们跟踪您的代码:

counter = 0

while counter == 0:
    print(ver1[0])
    counter += 1

当您第一次点击 while 时,counter 将为 0,因此它将运行一次。然后将 `counter 递增到 1,这样它就不会再次运行。

因此,这与您离开 while 循环并只编写以下内容具有相同的效果:

print(ver1[0])
counter += 1

下一个循环是一样的:

while counter == 1:
    print(ver1[1])
    counter += 1

这将始终恰好运行一次,因此您不需要循环。

其余的依此类推。

这意味着当您到达循环的末尾时,counter 将始终为 4

因此,您可以将所有代码替换为:

print(ver1[0])
print(ver1[1])
print(ver1[2])
print(ver1[3])
counter = 4

或者,您可以使用 while 来避免重复四次,如下所示:

counter = 0
while counter < 4:
    print(ver1[counter])
    counter += 1

虽然确实如此,但使用 for 语句可能会更好:

for counter in range(4):
    print(ver1[counter])

或者,甚至更好:

for ver in ver1:
    print(ver)

这些中的任何一个都可以以不同的方式扩展以处理不仅仅是 verse-1 行:

counter = 0
while counter < len(ver1):
    print(ver1[0])
    counter += 1
counter = 0
while counter < len(chor1):
    print(chor1[counter])
    counter += 1
# etc.

……或者……

all_lines = ver1 + chor1 + ver2 + chor1
counter = 0
while counter < len(all_lines):
    print(all_lines[counter])
    counter += 1

……或者……

for line in ver1:
    print(line)
for line in chor1:
    print(line)
# etc.

……或者……

for part in (ver1, chor1, ver2, chor2):
    for line in part:
        print(line)

……或者……

all_lines = ver1 + chor1 + ver2 + chor1
for line in all_lines:
    print(line)

……或者,如果您真的想让老师知道您远远领先于类(class)其他同学,或者上交了您在互联网上找到的您不理解的代码……

import itertools
print(*itertools.chain(ver1, chor1, ver2, chor1), sep='\n')

关于使用 while 循环打印输出的 Python 作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51314944/

相关文章:

python - 用椭圆显示矢量随时间的旋转

python - 查找按原点分隔的 2 个集合的对称差异

python-3.x - Python 3多处理和openCV问题与处理器之间的字典共享

python - 使用 asyncio 和 contextvars 在 python 中的两个异步程序之间共享状态

c - 在 C 中使用 while() 测试不等式

javascript - Python Crawling Pastebin(JavaScript 呈现的网页)

python - 为什么 zip 急切地评估其参数的元素?

python - 修改 Pandas 组

python - 提取字母并填充单词的位置

php while 在 for 循环内,使用 strlen