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

Discussion in 'Python' started by Id0x, Jul 20, 2003.

  1. Id0x

    Id0x Guest

    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.



    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();
     
    Id0x, Jul 20, 2003
    #1
    1. Advertising

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

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

    --
    Erik Max Francis && && 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
     
    Erik Max Francis, Jul 20, 2003
    #2
    1. Advertising

  3. Id0x

    Lee Harr Guest

    > 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
     
    Lee Harr, Jul 20, 2003
    #3
  4. Id0x

    klappnase Guest

    (Id0x) wrote in message news:<>...

    > 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
     
    klappnase, Jul 21, 2003
    #4
  5. 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 && && 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
    \__/ Sade
     
    Erik Max Francis, Jul 21, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet

    what's wrong in this program

    VisionSet, Sep 24, 2004, in forum: Java
    Replies:
    12
    Views:
    564
    Thomas G. Marshall
    Sep 28, 2004
  2. Replies:
    7
    Views:
    432
  3. Matthew
    Replies:
    7
    Views:
    873
    Priscilla Walmsley
    Jan 7, 2005
  4. jose luis fernandez diaz

    why this program is wrong on Tru64 OS ?

    jose luis fernandez diaz, Nov 25, 2003, in forum: C++
    Replies:
    2
    Views:
    450
    Stephan Br?nnimann
    Nov 26, 2003
  5. Replies:
    5
    Views:
    1,617
Loading...

Share This Page