3

When the below portion of the script is activated, it shows all the commas and single quotes (and parentheses) in the results.

print(name, 'has been alive for', days, 'days', minutes, 'minutes and', seconds, 'seconds!')

So, for instance:

('Ryan', 'has been alive for', 10220, 'days', 14726544, 'minutes and', 883593928, seconds!')

I want to clean it up, so it looks good. Is that possible?

By "good", I mean something like this:

Ryan has been alive for 10220 days, 14726544 minutes, and 883593928 seconds!

Here is the full script:

print("Let's see how long you have lived in days, minutes, and seconds!")
name = raw_input("name: ")

print("now enter your age")
age = int(input("age: "))

days = age * 365
minutes = age * 525948
seconds = age * 31556926

print(name, 'has been alive for', days, 'days', minutes, 'minutes and', seconds, 'seconds!')

raw_input("Hit 'Enter' to exit!: ")
Ryan Joachim
  • 81
  • 1
  • 6
  • 5
    if you're using python2.x, drop the parenthesis in the `print` statement. – isedev Mar 07 '14 at 23:03
  • 1
    This may not matter for a program like this (seems like you're just messing around with Python maybe), but one benefit to using the __future__ import over dropping the parens is that it will work in late Python2 as well as early Python3. I personally still use the parens for print in all my Python 2.7 stuff because it is more forward-compatible. – Two-Bit Alchemist Mar 07 '14 at 23:09
  • Using string formatting would be a good idea. – Matthias Mar 07 '14 at 23:14
  • I am definitely just playing around with Python. This was just a way to help me wrap my mind around how some things work in the language, as I've decided to choose Python as my first programming language. Thanks for all the suggestions! – Ryan Joachim Mar 07 '14 at 23:26

1 Answers1

7

You need from __future__ import print_function.

In Python 2.x what you are doing is interpreted as printing a tuple, while you are using the 3.x syntax.

Example:

>>> name = 'Ryan'
>>> days = 3
>>> print(name, 'has been alive for', days, 'days.')
('Ryan', 'has been alive for', 3, 'days.')
>>> from __future__ import print_function
>>> print(name, 'has been alive for', days, 'days.', sep=', ')
Ryan, has been alive for, 3, days.
Two-Bit Alchemist
  • 17,966
  • 6
  • 47
  • 82
  • Adding from __future__ import print_function at the very beginning of the script removed all of the quotes and parentheses (thanks!!), but it also removed the commas. Can I choose to keep those? – Ryan Joachim Mar 07 '14 at 23:26
  • 1
    Yes, but you will have to print them as strings. – Hoppo Mar 07 '14 at 23:45
  • @RyanJoachim It sounds like what you actually want is answered [here](http://stackoverflow.com/questions/5445970/printing-list-in-python-properly) – SethMMorton Mar 08 '14 at 01:00
  • 1
    @RyanJoachim Sorry I didn't check back this weekend. Using the Python 3 syntax it's easy to achieve what it sounds like you want by passing in the sep keyword, which stands for separator. It defaults to just a space but can be anything you want. I edited the example above to show you how to set it to comma space instead. – Two-Bit Alchemist Mar 10 '14 at 16:08