99 Bottles of Beer/Python: Difference between revisions

Content added Content deleted
m (Fixed syntax highlighting.)
 
Line 5: Line 5:


===Normal Code===
===Normal Code===
<lang python>def sing(b, end):
<syntaxhighlight lang="python">def sing(b, end):
print(b or 'No more','bottle'+('s' if b-1 else ''), end)
print(b or 'No more','bottle'+('s' if b-1 else ''), end)


Line 12: Line 12:
sing(i, 'of beer,')
sing(i, 'of beer,')
print('Take one down, pass it around,')
print('Take one down, pass it around,')
sing(i-1, 'of beer on the wall.\n')</lang>
sing(i-1, 'of beer on the wall.\n')</syntaxhighlight>


===Using a template===
===Using a template===
<lang python>verse = '''\
<syntaxhighlight lang="python">verse = '''\
%i bottles of beer on the wall
%i bottles of beer on the wall
%i bottles of beer
%i bottles of beer
Line 23: Line 23:


for bottles in range(99,0,-1):
for bottles in range(99,0,-1):
print verse % (bottles, bottles, bottles-1) </lang>
print verse % (bottles, bottles, bottles-1) </syntaxhighlight>


===New-style template (Python 2.6)===
===New-style template (Python 2.6)===
<lang python>verse = '''\
<syntaxhighlight lang="python">verse = '''\
{some} bottles of beer on the wall
{some} bottles of beer on the wall
{some} bottles of beer
{some} bottles of beer
Line 34: Line 34:


for bottles in range(99,0,-1):
for bottles in range(99,0,-1):
print verse.format(some=bottles, less=bottles-1) </lang>
print verse.format(some=bottles, less=bottles-1) </syntaxhighlight>


==="Clever" generator expression===
==="Clever" generator expression===
<lang python>a, b, c, s = " bottles of beer", " on the wall\n", "Take one down, pass it around\n", str
<syntaxhighlight lang="python">a, b, c, s = " bottles of beer", " on the wall\n", "Take one down, pass it around\n", str
print "\n".join(s(x)+a+b+s(x)+a+"\n"+c+s(x-1)+a+b for x in xrange(99, 0, -1))</lang>
print "\n".join(s(x)+a+b+s(x)+a+"\n"+c+s(x-1)+a+b for x in xrange(99, 0, -1))</syntaxhighlight>


===Enhanced "Clever" generator expression using lambda===
===Enhanced "Clever" generator expression using lambda===
<lang python>a = lambda n: "%u bottle%s of beer on the wall\n" % (n, "s"[n==1:])
<syntaxhighlight lang="python">a = lambda n: "%u bottle%s of beer on the wall\n" % (n, "s"[n==1:])
print "\n".join(a(x)+a(x)[:-13]+"\nTake one down, pass it around\n"+a(x-1) for x in xrange(99, 0, -1))</lang>
print "\n".join(a(x)+a(x)[:-13]+"\nTake one down, pass it around\n"+a(x-1) for x in xrange(99, 0, -1))</syntaxhighlight>


===Using a generator expression (Python 3)===
===Using a generator expression (Python 3)===
<lang python>#!/usr/bin/env python3
<syntaxhighlight lang="python">#!/usr/bin/env python3
"""\
"""\
{0} {2} of beer on the wall
{0} {2} of beer on the wall
Line 58: Line 58:
"bottle" if i - 1 == 1 else "bottles"
"bottle" if i - 1 == 1 else "bottles"
) for i in range(99, 0, -1)
) for i in range(99, 0, -1)
), end="")</lang>
), end="")</syntaxhighlight>


===A wordy version===
===A wordy version===
<lang python>ones = (
<syntaxhighlight lang="python">ones = (
'', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'
'', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'
)
)
Line 99: Line 99:
print takeonedown
print takeonedown
print bottles(beer-1), onthewall
print bottles(beer-1), onthewall
print</lang>
print</syntaxhighlight>


===String Formatting===
===String Formatting===
<lang python>for n in xrange(99, 0, -1):
<syntaxhighlight lang="python">for n in xrange(99, 0, -1):
## The formatting performs a conditional check on the variable.
## The formatting performs a conditional check on the variable.
## If it formats the first open for False, and the second for True
## If it formats the first open for False, and the second for True
Line 108: Line 108:
print n, 'bottle%s of beer.' % ('s', '')[n == 1]
print n, 'bottle%s of beer.' % ('s', '')[n == 1]
print 'Take one down, pass it around.'
print 'Take one down, pass it around.'
print n - 1, 'bottle%s of beer on the wall.\n' % ('s', '')[n - 1 == 1]</lang>
print n - 1, 'bottle%s of beer on the wall.\n' % ('s', '')[n - 1 == 1]</syntaxhighlight>


===Python 3 f-strings and walrus operator===
===Python 3 f-strings and walrus operator===
<lang python>bottles = 100
<syntaxhighlight lang="python">bottles = 100
while (bottles:=bottles-1) != 0:
while (bottles:=bottles-1) != 0:
print(f"{bottles} bottles of beer on the wall\n{bottles} bottles of beer\nTake one down, pass it around\n{bottles-1} bottles of beer on the wall\n")</lang>
print(f"{bottles} bottles of beer on the wall\n{bottles} bottles of beer\nTake one down, pass it around\n{bottles-1} bottles of beer on the wall\n")</syntaxhighlight>