-3

I want to be able to print this in to a text file, however I have looked around and can't figure out what I need to do.

def countdown (n):
    while (n > 1):
        print('\n',(n), 'Bottles of beer on the wall,', (n), 'bottles of beer, take one down pass it around', (n)-1, 'bottles of beer on the wall.')
        n -= 1
        if (n == 2):
            print('\n',(n), 'Bottles of beer on the wall,', (n), 'bottles of beer, take one down pass it around', (n)-1, 'bottle of beer on the wall.')
        else:
            print ('\n',(n), 'Bottle of beer on the wall,', (n), 'bottle of beer, take one down pass it around no more bottles of beer on the wall.')

countdown (10)
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Nataku62
  • 33
  • 1
  • 8
  • **I have looked around**, and what have you tried? SO is not a code-writing service. Please work on your problem and come back with some code. – Swastik Padhi Nov 08 '15 at 01:03
  • It would be nice if you do some web browsing to get the answer to this question – repzero Nov 08 '15 at 01:14

2 Answers2

3

Instead of...

...
print('123', '456')

Use...

myFile = open('123.txt', 'w')
...
print('123', '456', file = myFile)
...
myFile.close() # Remember this out!

Or even...

with open('123.txt', 'w') as myFile:
    print('123', '456', file = myFile)

# With `with`, you don't have to close the file manually, yay!

I hope this has led some light on you!

3442
  • 8,248
  • 2
  • 19
  • 41
0

To be more "correct" it would be considered writing to a text file. You can code something like this:

def countdown (n):
    # Open a file in write mode
    file = open( 'file name', 'w')
    while (n > 1):
        file.write('\n',(n), 'Bottles of beer on the wall,', (n), 'bottles of beer, take one down pass it around', (n)-1, 'bottles of beer on the wall.')
        n -= 1
        if (n == 2):
            file.write('\n',(n), 'Bottles of beer on the wall,', (n), 'bottles of beer, take one down pass it around', (n)-1, 'bottle of beer on the wall.')
        else:
            file.write('\n',(n), 'Bottle of beer on the wall,', (n), 'bottle of beer, take one down pass it around no more bottles of beer on the wall.')

    # Make sure to close the file, or it might not be written correctly.
    file.close()


countdown (10)
Craig
  • 55
  • 7
  • 2
    ick let's not shadow builtins like `file`. Usually I see `f`, `inf`, or `outf` if there's no better description than "file". – Adam Smith Nov 08 '15 at 01:10
  • @AdamSmith Actually there is **no** `file` built-in function in Python 3.x. But I agree about use `f` instead of `file`. – Remi Guan Nov 08 '15 at 01:12
  • I was unaware of any builtins.. Was simply making it more readable. I agree with naming it f or whatever else. – Craig Nov 08 '15 at 01:33
  • @KevinGuan I actually didn't realize they did away with that in Python3 (I never found a good use for it anyway). Thanks for the heads up – Adam Smith Nov 08 '15 at 01:42
  • @AdamSmith Yeah, [here](http://stackoverflow.com/questions/112970/python-when-to-use-file-vs-open) is a good SO question :) – Remi Guan Nov 08 '15 at 01:48