Created
January 14, 2020 11:47
-
-
Save TheMuellenator/a36e7edadcf4b38aff1d759dad737526 to your computer and use it in GitHub Desktop.
Python Loops Coding Exercise Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sing(num_bottles): | |
#TODO: Add your code to achieve the desired output and pass the challenge. | |
#NOTE: The f String method of String Interpolation does not work. | |
lyrics = [] | |
for num in range(num_bottles, 0, -1): | |
lyrics.append('{num} bottles of beer on the wall, {num} bottles of beer.'.format(num = num)) | |
lyrics.append('Take one down and pass it around, {num} bottles of beer on the wall.'.format(num = num - 1)) | |
lyrics.append('') | |
return lyrics | |
Here is another alternative
def sing(num_bottles):
lirics = []
for n in range(0, num_bottles, 1):
if num_bottles - n == 0:
break
lirics.append(f'{num_bottles - n} bottles of beer on the wall. {num_bottles - n} bottles of beer')
lirics.append(f'Take one down and pass it around, {num_bottles - n-1} bottles of beer on the wall')
lirics.append('')
return lirics
I don't understand the notation with curly braces why we need them and how it works,
why we need to use syntax .format(num = num)
why we need to specifiy .format(num = num -1) to decrease with -1,
why is not enough to have only in function range() the third parameter which his purpose is exactly to specify increment/decrement step
I've got issue with exercise 6. My answer was correct and still saying wrong...
Here's another example that's a bit simpler:
def sing(num_bottles):
output = []
for n in reversed(range(num_bottles + 1)):
if(n == 0) :
break;
output.append('{} bottles of beer on the wall, {} bottles of beer.'.format(n, n))
output.append('Take one down and pass it around, {} bottles of beer on the wall.'.format(n - 1))
output.append('');
return output
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is an alternative