First thingy in Python

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

Korvost
Posts: 11
Joined: Mon Feb 25, 2008 11:32 pm UTC

First thingy in Python

Postby Korvost » Mon Feb 25, 2008 11:40 pm UTC

As the title implies, this is my first little thingy i've written in Python wich isn't a example in the book i'm learning from.

Code: Select all

# Creepiness rule minimum dating age calculator
# Based on a xkcd comic - http://xkcd.com/314/
from __future__ import division
age = input("How old are you?\n")
minage = age / 2 + 7
print
print 'The age of your date should at minimum be:'
print minage
raw_input("")


(I know i could make it shorter, give me some advice, please?)

HappySmileMan
Posts: 52
Joined: Fri Nov 09, 2007 11:46 pm UTC

Re: First thingy in Python

Postby HappySmileMan » Mon Feb 25, 2008 11:49 pm UTC

Korvost wrote:As the title implies, this is my first little thingy i've written in Python wich isn't a example in the book i'm learning from.

Code: Select all

# Creepiness rule minimum dating age calculator
# Based on a xkcd comic - http://xkcd.com/314/
age = input("How old are you?\n")
minage = age / 2 + 7
print '\nThe age of your date should at minimum be:' + minage


(I know i could make it shorter, give me some advice, please?)


I don't think you need to import division
You can print a '\n' character and it adds an empty line.
And you can concatenate things with a '+' operator.
The raw_input() at the end probably stops the command line from closing on you, so maybe that's useful to you, but I removed it since I just call my scripts from command line. Try it with the raw_input if that doesn't work

I haven't tested this but I'm pretty sure it works.

Korvost
Posts: 11
Joined: Mon Feb 25, 2008 11:32 pm UTC

Re: First thingy in Python

Postby Korvost » Mon Feb 25, 2008 11:57 pm UTC

For some reason, it doesn't work. :(
"TypeError: cannot concatenate 'str' and 'int' objects"

Also, i'm going to sleep now, i'll check this thread tomorrow.

HappySmileMan
Posts: 52
Joined: Fri Nov 09, 2007 11:46 pm UTC

Re: First thingy in Python

Postby HappySmileMan » Tue Feb 26, 2008 12:00 am UTC

Korvost wrote:For some reason, it doesn't work. :(
"TypeError: cannot concatenate 'str' and 'int' objects"

Also, i'm going to sleep now, i'll check this thread tomorrow.


Code: Select all

print '\nThe age of your date should at minimum be:' + str(minage)


That'll convert minage to a string and then join it to the string and print it

User avatar
Berengal
Superabacus Mystic of the First Rank
Posts: 2707
Joined: Thu May 24, 2007 5:51 am UTC
Location: Bergen, Norway
Contact:

Re: First thingy in Python

Postby Berengal » Tue Feb 26, 2008 2:06 am UTC

Or the wonderful string comprehension:

Code: Select all

print '\nThe age of your date should at minimum be: %d' % minage


I like it, since it can be used anywhere. Lately I've been doing too much of 'interface.printString(String.format(...));' in java...
It is practically impossible to teach good programming to students who are motivated by money: As potential programmers they are mentally mutilated beyond hope of regeneration.

Permagreen
Posts: 3
Joined: Sat Feb 16, 2008 11:18 pm UTC

Re: First thingy in Python

Postby Permagreen » Tue Feb 26, 2008 4:44 am UTC

Okay, I'm probably about as new to Python as Korvost, but I actually did the same program last week and came up with this:

Code: Select all

def date():
    n = input('How old are you? ')   
    c1 = (n/2)+ 7
    c2 = (n*2)-14
    print "You shouldn't date someone under age", c1, "or over age", c2


I found the upper bound formula in the topic for the comic. Also I know it doesn't need to be a defined function, but that made it a lot easier to play around with. It may not be the best bit of Python code there is, but it works.

User avatar
e946
Posts: 621
Joined: Wed Jul 11, 2007 6:32 am UTC

Re: First thingy in Python

Postby e946 » Tue Feb 26, 2008 6:02 am UTC

Just to nitpick, how did you come up with c1 and c2 for variable names?

joeframbach
Posts: 1478
Joined: Sun Nov 05, 2006 12:49 am UTC

Re: First thingy in Python

Postby joeframbach » Tue Feb 26, 2008 6:05 am UTC

e946 wrote:Just to nitpick, how did you come up with c1 and c2 for variable names?

and n for age?

User avatar
phlip
Restorer of Worlds
Posts: 7573
Joined: Sat Sep 23, 2006 3:56 am UTC
Location: Australia
Contact:

Re: First thingy in Python

Postby phlip » Tue Feb 26, 2008 6:27 am UTC

HappySmileMan wrote:I don't think you need to import division

__future__ is a magic module that lets you start using functionality that will become default in future versions of python... in this case, it's making floating point division the default even with integer parameters (which is pretty standard in most dynamically-typed languages). The old way, which makes int/int return int (rounded down) will be gone in Python 3.

Try:

Code: Select all

print 'old 5/2:', 5/2 # prints 2

Code: Select all

from __future__ import division
print 'new 5/2:', 5/2 # prints 2.5
print 'The new way will be mandatory as of %d.%d.%d %s %d" % division.getMandatoryRelease() # prints 3.0.0 alpha 0 in my Python build


Check out the doc for the __future__ magic and the doc for the module itself.

Code: Select all

enum ಠ_ಠ {°□°╰=1, °Д°╰, ಠ益ಠ╰};
void ┻━┻︵​╰(ಠ_ಠ ⚠) {exit((int)⚠);}
[he/him/his]

Korvost
Posts: 11
Joined: Mon Feb 25, 2008 11:32 pm UTC

Re: First thingy in Python

Postby Korvost » Tue Feb 26, 2008 7:05 pm UTC

Thanks for the tips everyone!

Permagreen
Posts: 3
Joined: Sat Feb 16, 2008 11:18 pm UTC

Re: First thingy in Python

Postby Permagreen » Wed Feb 27, 2008 2:36 am UTC

joeframbach wrote:
e946 wrote:Just to nitpick, how did you come up with c1 and c2 for variable names?

and n for age?


Sorry, I'm a bit of a newbie at the whole programming thing (I started about... maybe a little less than a month ago), so generally my variables aren't as clear as they should be. I use n because a lot of the books I've been reading use n for the simple little 'do this stuff to this number' programs, so the n stands for number. As for the c1 and c2, when I first wrote the program I just had the formula for the lower bound, which I arbitrarily assigned to the letter c. After I discovered the upper bound formula I wanted to make it clear the two were related, hence c1 and c2.

I probably should have changed them to something less random before posting, but it just didn't seem all that important for 5 lines of code.

Dakman
Posts: 50
Joined: Sat Jul 07, 2007 7:49 am UTC

Re: First thingy in Python

Postby Dakman » Wed Feb 27, 2008 2:48 pm UTC

People.... stop using input() to grab integers! input should only be used to inject code to the interpreter during runtime. It's not fail safe for integers, you could still enter a string.

Code: Select all

while True:
    try:
        age = int(raw_input("What is your age? ")
        break
    except:
        print "Please enter a valid integer (0-9)."

Oh and just for fun (without error checking the integer conversion)

Code: Select all

print "\nThe minimum age you should date is: %i" % (int(raw_input("Age: ")) / 2 + 7)

zahlman
Posts: 638
Joined: Wed Jan 30, 2008 5:15 pm UTC

Re: First thingy in Python

Postby zahlman » Sat Mar 01, 2008 11:24 pm UTC

Permagreen wrote:so generally my variables aren't as clear as they should be. I use n because a lot of the books I've been reading use n for the simple little 'do this stuff to this number' programs, so the n stands for number. As for the c1 and c2, when I first wrote the program I just had the formula for the lower bound, which I arbitrarily assigned to the letter c. After I discovered the upper bound formula I wanted to make it clear the two were related, hence c1 and c2.


An important principle: if you can't think of a good name for something, it likely isn't important enough to be named. So see if you can get away with not naming it. Here, you do that by just substituting in the expressions in-line in the print statement.

Code: Select all

# By reading the number as a float, we get floating-point division. If an integer is input,
# 'float' can still construct a float from it. And why shouldn't I claim to be 26.8 years old?
age = None
while age == None:
  try: age = float(raw_input("How old are you?\n"))
  except ValueError: print "Give your age as a number, please.\n"
print "It is not creepy for you to date people between %d and %d", (age / 2 + 7, (age - 7) * 2)
Belial wrote:I once had a series of undocumented and nonstandardized subjective experiences that indicated that anecdotal data is biased and unreliable.

post-l33t
Posts: 2
Joined: Thu Mar 06, 2008 6:03 pm UTC

Re: First thingy in Python

Postby post-l33t » Thu Mar 06, 2008 6:05 pm UTC

Where are the one-liner fans?

Code: Select all

print "Don't date anyone < %i years old" % (input("How old are you?") / 2 + 7)


Return to “Coding”

Who is online

Users browsing this forum: Majestic-12 [Bot] and 5 guests