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

I

Id0x

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.

(e-mail address 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();
 
E

Erik Max Francis

Id0x said:
##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

instead of

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" :).
 
L

Lee Harr

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
 
K

klappnase

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
 
E

Erik Max Francis

klappnase said:
The "None" comes from:


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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top