【问题标题】:Python homework with while loops to print outputPython作业与while循环打印输出
【发布时间】:2018-07-12 21:47:19
【问题描述】:

我正在尝试学习“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)

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

【问题讨论】:

  • 我的赞美。您是少数具有您的声誉值的用户之一,他们会阅读自己的问题并检查适当的格式。这是一个快速而好的编辑。非常欢迎使用 StackOverflow。
  • 这对我来说应该是一个for 循环——您是否特别关注while?还是一般来说只是循环?
  • 很多老师似乎坚持先教while,再教for。然后,他们没有考虑使用while 的好例子,而是给学生一堆只是乞求for 的作业,但坚持要用while 代替。所以更聪明的学生(可能像这个学生)直觉地知道他们缺少一些东西——他们想要for声明,即使他们还不知道,所以他们觉得像 Python 一样,而不是他们的老师,正在限制他们的表现力。这只是一种糟糕的教人方式。
  • while 循环特别.. 虽然我注意到当我搜索 'while'..while/for 似乎是主题时,但我注意到我们没有涵盖 'for' 语句。
  • 谢谢大家。我发布了我的最终代码作为答案。感谢所有帮助。

标签: python python-3.x while-loop


【解决方案1】:

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

实际上,您过度使用 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)

这些中的任何一个都可以通过不同的方式扩展以处理不仅仅是第 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)

…或者,如果你真的想让你的老师知道你要么领先于班上的其他人太远,要么提交你在互联网上找到的你不理解的代码......

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

【讨论】:

  • 这是不允许的,因为它不包括“while”的使用。老师开玩笑地问我当我完成课堂作业时,我是否使用所有打印语句作弊。 code print(ver1[0]) print(ver1[1]) print(ver1[2]) print(ver1[3]) counter = 4 对不起,它没有被正确阻止..我正在尝试使用@ 987654344@降价,但它仍在变平
  • 使用你的 while 计数器 - print(ver1[counter]) - 我只是得到一个无限循环。
  • @RobertL78 但是在没有什么可循环的情况下使用while 实际上并没有使用while。您不妨每隔一行在代码中添加while False: assert False, "This is impossible!" 语句。 “或者,交替”显示了如何实际使用 while
  • @RobertL78 并且while counter &lt; 4: 循环肯定不会永远循环,除非你错过了counter += 1,或者缩进不当,或者类似的东西。 You can see it working on repl.it.
  • 你是对的@abarnert 我未能将 1 添加到计数器。一点点进步。
【解决方案2】:

因为是作业,所以我不会给你代码。在这里使用 while 循环实际上是反直觉的,但它确实有效。

如果计数器是 0/1/2/3,那就是诗句。否则,如果它是 4,9,10,它是 chrous。否则,如果它的 5/6/7/8 它是 verse2。因此,您可以在 while 循环内有一个带有 3 个 if 语句的 while 循环,用于不同的场景。所以每次在这个 while 循环中,你检查 3 个场景,然后增加计数器。

如果您想使用 4 个 while 循环,abarnert 有一个很好的解决方案。但是,您可以在一个 while 循环中使用我所写的内容,但您需要 IF 语句。

【讨论】:

    【解决方案3】:

    感谢大家的帮助。我终于让它以正确的输出正常运行,尽管我觉得在第一节之后我仍然通过将计数器重置为 0 来作弊。但也许因为这只是我的第二堂课,所以可以接受。我很确定我不必这样做,但我无法弄清楚如何在不重置计数器的情况下使用 print(ver2[counter]) 。否则我会得到一个“列表索引超出范围”错误,因为 ver2 是一个不同的变量,并且 ver2 的列表条目的索引从 0 开始。

    ver1 = ['sentence1', 'sentence2', 'sentence3', 'sentence4']
    ver2 = ['sentence5', 'sentence6', 'sentence7', 'sentence8']
    chor1= ['chorus1', 'chorus2']
    
    counter = 0
    
    while counter < 4:
        print(ver1[counter])
        counter += 1
    
    if counter == 4:
        print(chor1[0])
        counter += 1
    
    counter = 0
    
    while counter < 4:
        print(ver2[counter])
        counter += 1
    
    if counter == 4:
        print(*chor1, sep = "\n")
    #I googled around a bit to find a way to print the items in chor1 with a 
    #line return so it lined up with the rest instead of printing side by 
    #side
    #I do see now where @abarnert showed the sep = "\n" to return the line
    #though I missed it in all the code options listed. 
    

    【讨论】:

    • ver2 的列表条目的索引从 0 开始,但是您可以通过执行 ver2[counter-4] 来执行此操作而无需重置计数器,因此当计数器为 4、5、6、7 时,您将访问ver2[0,1,2,3]
    • 太棒了。谢谢你。我从这篇文章中学到了很多东西。
    猜你喜欢
    • 2021-02-26
    • 2016-01-15
    • 2018-04-20
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多