RobertL78
RobertL78

Reputation: 29

Python homework with while loops to print output

I am trying to learn 'while' loops and counters. I understand how to use them on a basic level but I feel like I'm over using them in this case and there may be a better beginner answer that still uses while loops and if/elif/else statements.

Basically the program should print each sentence in a verse based on the counter starting at 0 and printing sentence1, then after the 4th sentence it prints the chorus... Then it moves on to print the next 4 sentences and then the chorus twice at the end.

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.

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])

I created it with if statements to start with but the teacher asked if I'd try making it with while loops as a homework assignment... Here is the original if/elif/else statement that I wrote.

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 just the first part of the program because I didn't want to continue with it and waste my time if there was a better answer than just copy/pasting while loops with a couple small edits to determine what gets printed.

Upvotes: 3

Views: 643

Answers (3)

RobertL78
RobertL78

Reputation: 29

Thank you everyone for your help. I finally got it to run properly with the correct output though I feel like I still cheated a bit by resetting the counter to 0 after the first verse. But maybe since this is only my 2nd class it's acceptable. I'm pretty sure I didn't have to but I couldn't figure out how to use print(ver2[counter]) without resetting the counter. Otherwise I would get a 'list index out of range' error since ver2 is a different variable and the index for the list entries of ver2 start at 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. 

Upvotes: 0

abarnert
abarnert

Reputation: 365607

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.

Actually, you're overusing the while loops making it more complicated.

Let's trace through your code:

counter = 0

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

When you first hit that while, counter will be 0, so it will run once. Then you'll increment `counter to 1, so it won't run again.

So, this has the same effect as if you'd left the while loop off and just written this:

print(ver1[0])
counter += 1

And the next loop is the same:

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

This will always run exactly one time, so you don't need the loop.

And so on for the rest of them.

Which means that when you get to the end of the loops, counter will always be 4.

So, you could replace all of that code with:

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

Or, alternatively, you could get some use out of while to avoid repeating yourself four times, like this:

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

Although really, this would probably be better written with a for statement:

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

Or, even better:

for ver in ver1:
    print(ver)

Any of these can be extended to handle more than just the verse-1 lines, in different ways:

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

… or …

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

… or …

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

… or …

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

… or …

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

… or, if you really want your teacher to know you're either way too far ahead of the rest of the class, or turning in code you found on the internet that you don't understand …

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

Upvotes: 4

WebOrNative
WebOrNative

Reputation: 103

Since it's homework I wont give you the code. Using while loop here is actually counter intuitive but it works.

If the counter is 0/1/2/3, it's verse. else if its 4,9,10, it's chrous. else if its 5/6/7/8 it's verse2. So you could have a while loop with 3 if statements inside the while loop that for the different scenarios. So each time in this while loop, you check for the 3 scenarios and then increase the counter.

abarnert has a great solution, if you want to use 4 while loops. However you can use what I wrote about in one while loop, but you would need IF statements.

Upvotes: 1

Related Questions