0

This is my first python program. It asks for your name, age, and how many days are left until your birthday. It then gives you how many days old you are. I've added some write functions so that it writes the data into another file (input.txt) Is this code clean or could it be more concise? I was wondering especially about changing the strings and integers part.

from sys import argv
script, filename = argv

target = open(filename, 'a+')
name = raw_input("What is your name?\n")
print "Hello, %s" % name

years = int(raw_input("How old are you in years?\n"))
extra_days = int(raw_input("How many days until your birthday?\n"))
days = years * 365.25 + 365 - extra_days
print "You are %r days old" % int(days)
length = len(name)
print "Your name is %d letters long." % length

target.write(name + " is " + str(days) + "days old.\n")

target.close()
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
ben
  • 55
  • 2
  • 9

2 Answers2

1

Here's how I'd write it:

import sys # simple import is clearer when used below
script, filename = sys.argv

with open(filename, 'a+') as target: # will automatically close() at the end
    name = raw_input("What is your name?\n")
    print "Hello,", name

    years = int(raw_input("How old are you in years?\n"))
    extra_days = int(raw_input("How many days until your birthday?\n"))
    days = years * 365.25 + 365 - extra_days
    print "You are", days, "days old"
    length = len(name)
    print "Your name is", length, "letters long."

    print >>target, name, "is", days, "days old." # auto newline and formatting

If you prefer the ex situ formatting of strings as you had with %, you should at least switch to the newer style which is like "The answer is {}.".format(42). This way you need not worry about which format specifier to use, and it gives you some other features as well.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1
with open(filename,"a+") as target:
    name = raw_input("What is your name?\n")
    print "Hello, {}".format(name)

    years = int(raw_input("How old are you in years?\n"))
    extra_days = int(raw_input("How many days until your birthday?\n"))
    days = years * 365.25 + 365 - extra_days
    print "You are {} days old".format(days)
    print "Your name is {} letters long.".format(len(name))

    target.write("{} is {} days old".format(name,str(days)))

This is better, used to use format() function :-). Also with open is much better, you don't have to care about your files process. And better you catch some errors, you can't know maybe user push any string on years , so;

with open(filename,"a+") as target:
    try:
        name = raw_input("What is your name?\n")
        print "Hello, {}".format(name)

        years = int(raw_input("How old are you in years?\n"))
        extra_days = int(raw_input("How many days until your birthday?\n"))
        days = years * 365.25 + 365 - extra_days
        print "You are {} days old".format(days)
        print "Your name is {} letters long.".format(len(name))

        target.write("{} is {} days old".format(name,str(days)))
    except:
        print ("An error occured, please try again.")
  • is using this type of 'format()' universal? is it similar to '%r'? – ben Jan 14 '15 at 02:19
  • It's similar to %r, much better. Check some documentation of `format()` function. Here a question: http://stackoverflow.com/questions/5082452/python-string-formatting-vs-format –  Jan 14 '15 at 02:20