Velocity Reviews > Newbie! I'm a newbie! What's wrong with this program?

Newbie! I'm a newbie! What's wrong with this program?

Id0x
Guest
Posts: n/a

 07-20-2003
I'm very new at programing, and while I was practicing I came across
something that confuses me. The code for the program is at the bottom.
The program it self is designed to convert hours to minutes. Now the
problem is, after I run the program I get strange results.

[\| Running the Program |/]

INPUT:
Ms-Dos Prompt:
C:\python23\> python practice.py

OUTPUT:

There are 300
None minutes in 5 hours <--- Problem occurs here

[\|End Running of Program|/]

Now the problem is... where does the "None" come from? And why is it
there? Here is the code. Please email me if you can. Your help is
appreciated.

http://www.velocityreviews.com/forums/(E-Mail Removed)

import math ##loads the math modual
########Start created Functions#########
##Function for converting minutes to hours##
def minHour(x):
min = 60
hour = min * x
print hour
##Function creates a new line##
def newLine():
print
#######End created Functions########
###Start Variables####
x_hours = 5
###End Variables####
newLine();
print "There are", minHour(x_hours), "minutes in", x_hours, "hours";
newLine();
print "Program Terminated.";
newLine();

Erik Max Francis
Guest
Posts: n/a

 07-20-2003
Id0x wrote:

> ##Function for converting minutes to hours##
> def minHour(x):
> min = 60
> hour = min * x
> print hour

Your function does the conversion and then _prints_ the result. It
doesn't return anything.

> print "There are", minHour(x_hours), "minutes in", x_hours, "hours";

Here you're trying to print the return value of minHour, which since you
didn't return anything, defaults to None. Fix it by making that last
line of minHour

return hour

print hour

> newLine();
> print "Program Terminated.";
> newLine();

Note that Python does not require an end of line discriminator, so all
these semicolons are redundant (you seem to know this since since you
don't use them in some places, but use them in others). (Also,
"newLine()" really is a curious abbreviation for "print" .

--
Erik Max Francis && (E-Mail Removed) && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Love is a hole in the heart.
\__/ Ben Hecht

Lee Harr
Guest
Posts: n/a

 07-20-2003
> import math ##loads the math modual
> ########Start created Functions#########
> ##Function for converting minutes to hours##
> def minHour(x):
> min = 60
> hour = min * x
> print hour
> ##Function creates a new line##
> def newLine():
> print
> #######End created Functions########
> ###Start Variables####
> x_hours = 5
> ###End Variables####
> newLine();
> print "There are", minHour(x_hours), "minutes in", x_hours, "hours";
> newLine();
> print "Program Terminated.";
> newLine();

# mi.py
import math
import sys

def minutes_per_hour(hours):
MIN_PER_HOUR = 60
return hours * MIN_PER_HOUR

def print_minutes(hours):
minutes = minutes_per_hour(hours)
print "There are %s minutes in %s hours" % (minutes, hours)
print

if __name__ == '__main__':
hours = float(sys.argv[1])
print_minutes(hours)
#

>python mi.py 5.2

There are 312.0 minutes in 5.2 hours

>python mi.py 5

There are 300.0 minutes in 5.0 hours

>python mi.py 5.25

There are 315.0 minutes in 5.25 hours

klappnase
Guest
Posts: n/a

 07-20-2003
(E-Mail Removed) (Id0x) wrote in message news:<(E-Mail Removed). com>...

> There are 300
> None minutes in 5 hours <--- Problem occurs here
>
> [\|End Running of Program|/]
>
> Now the problem is... where does the "None" come from? And why is it
> there? Here is the code. Please email me if you can. Your help is
> appreciated.

The "None" comes from:

> def newLine():
> print

What you meant was:
def newLine():
print "\r"
(I must admit that I am not familiar with line endings on windoze, in
linux it is "\n", on windoze it might be "\r" or "\r\n", I don't know
for sure, however you don't need this function at all)

You might try something like:

print "There are " + str(minHour(x_hours)) + " minutes in " +
str(x_hours) + " hours"

Michael

Erik Max Francis
Guest
Posts: n/a

 07-20-2003
klappnase wrote:

> The "None" comes from:
>
> > def newLine():
> > print

>
> What you meant was:
> def newLine():
> print "\r"

No, that isn't where it comes from.

> (I must admit that I am not familiar with line endings on windoze, in
> linux it is "\n", on windoze it might be "\r" or "\r\n", I don't know
> for sure, however you don't need this function at all)

If sys.stdout is opened in text mode then '\n' will get translated to
the appropriate form regardless. So this is a red herring you're after.

> You might try something like:
>
> print "There are " + str(minHour(x_hours)) + " minutes in " +
> str(x_hours) + " hours"

That would still print exactly the same thing. The problem is that
minHour is returning None (implicitly).

--
Erik Max Francis && (E-Mail Removed) && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ All your slick moves / They were once innocent moves