5 Replies - 699 Views - Last Post: 10 October 2011 - 05:07 PM Rate Topic: -----

#1 apejam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-February 11

getting functions to play together.

Posted 10 October 2011 - 07:49 AM

Hello all. I have a school assignment that I could use some help on. I need to write a program that converts a user input distance to various other measurement units based on a "menu" with 4 choices. Seems simple enough, but I am limited in what I can use. The menu and every conversion needs to be a separate function and I cannot have any global variables.


here is what I have so far. it works and converts, but it asks the user to enter a number in twice. Is this because in my conversion function I am recalling the distance function that asks for the input?

I'm not sure if it is my understanding of functions thats lacking or what, hoping you guys can point me in the right direction.


def distance():
     distMeters =float(input('enter the distance in meters'))
     if distMeters <= 0:
         print ('that is an invalid distance, please enter another')    
     return distMeters
   
def kilometers():
    km = distance() * 0.001
    print('your distance conversion in kilometers is ' + str(km) + ' km')

def inches():
    inch = distance() * 39.37
    print (inch)

def feet():
    ft = distance() * 3.281
    print (ft)

def menu():

  

        print ('choose which units you would like to convert to from the menu')
        print ()
        print ('1.  Convert to kilometers')
        print ('2.  Convert to inches')
        print ('3.  Convert to feet')
        print ('4.  Exit the program')
 

def conversions():
   
        loop =1
        while loop ==1:

            choice = int(input('what menu option do you choose? '))
            print ()

            if choice >= 5:
                print ('that is not a choice in the listing. Try Again.')

            if choice == 1:
                print ('you chose to convert meters to kilometers')
                kilometers()
               

            if choice == 2:
                print ('you chose to convert meters to inches')
                inches()

            if choice == 3:
                print ('you chose to convert meters to feet')
                feet()
        

            if choice == 4:
               print ('Thank you for using the converter, program is now quitting')
               break 




conversions()
menu()





    





Is This A Good Question/Topic? 0
  • +

Replies To: getting functions to play together.

#2 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: getting functions to play together.

Posted 10 October 2011 - 08:27 AM

That is correct, every time you call distance() it will ask the user to enter the distance. You need to separate the code asking for input, and calculations into different sections (or functions) so you can call them when you need to.

Another thing, you say "that is an invalid distance, please enter another" but don't ask for it again, you return the value regardless. You want to only return the distance when it's > 0.

Not sure you want me to provide any code for you, so I'll leave it for now. Ask if you have any more questions :)

This post has been edited by Simown: 10 October 2011 - 08:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 apejam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-February 11

Re: getting functions to play together.

Posted 10 October 2011 - 09:00 AM

Thanks so much for the fast reply simown!

As far as I can tell the function to get just input and calculations are already separated? what do you mean?
also how could i get it to return only if its higher than 0 another if statement specifying that?

please do use code examples! it's easier for me to understand how something works by seeing how its implemented.
Was This Post Helpful? 0
  • +
  • -

#4 Simown  Icon User is offline

  • Blue Sprat
  • member icon

Reputation: 319
  • View blog
  • Posts: 650
  • Joined: 20-May 10

Re: getting functions to play together.

Posted 10 October 2011 - 09:29 AM

The function distance() takes the input and returns the distance, therefore you can't use it in the other functions, such as inches() and kilometers()

A better way would be to split them up even further:

def inputMeters():
    while True:
       distMeters =float(input('enter the distance in meters'))
       if distMeters > 0:
            return distMeters

def kilometers(meters):
    km = meters * 0.001
    return km

def inches(meters):
    inch = meters * 39.37
    return inch

#Then you could convert to any measurement without asking for the input again
# For example:

distMeters = inputMeters()
print('The distance in kilometers is ' + str(kilometers(distMeters)) + ' km.')
print('The distance in inches is ' + str(inches(distMeters)) + ' inches.')



You'll notice I fixed your input "problem" it will ask for the input as long as the number entered is <= 0. You only need 1 return for this, a return breaks out of the loop too.
while True: will keep going until the return statement is reached, and that's when distMeters > 0.

It doesn't check for wrong input such as Strings, do you need to do that?

:)

This post has been edited by Simown: 10 October 2011 - 09:33 AM

Was This Post Helpful? 1
  • +
  • -

#5 apejam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 12
  • Joined: 18-February 11

Re: getting functions to play together.

Posted 10 October 2011 - 04:40 PM

Once again thanks for your help simown! I now have a proper program! unfortunately I still have some questions. My main problem is I don't understand how to pass data from one function to another.

for instance, in the example you give me. how does the return of "distMeters" end up in the other functions "(meters)" parameters? I don't see anything specifying it. since it was returned before defining the other functions does it just go down the line and place itself in the empty parameters? is there something I am not seeing?







(the finished product)

def menu():
    print ('choose which units you would like to convert to from the menu')
    print ()
    print ('1.  Convert to kilometers')
    print ('2.  Convert to inches')
    print ('3.  Convert to feet')
    print ('4.  Exit the program')

def menuChoice():
    repeat = 0
    while repeat == 0:
        choice = int(input('from the menu choose a conversion '))
        print()

        if choice >=5:
            print ('that selection is not in the listing')

        if choice ==1:
            print ('your conversion to kilometers is ' + str(kilometers(distMeters)) +' km')

        if choice ==2:
            print ('your conversion to inches is ' + str(inches(distMeters)) + ' in')

        if choice ==3:
            print ('your conversion to feet is ' + str(feet(distMeters)) + ' ft')

        if choice ==4:
            print ('You are now exiting the program')
            break

def inputMeters():
    while True:
        distMeters =float(input('enter the distance in meters'))
        if distMeters > 0:
            return distMeters
        else:
            print('invalid distance')

def kilometers(meters):
    km = meters * 0.001
    return km

 def inches(meters):
    inch = meters * 39.37
    return inch

def feet(meters):
    ft = meters * 3.281
    return ft

distMeters = inputMeters()
menu()
menuChoice()




This post has been edited by apejam: 10 October 2011 - 04:42 PM

Was This Post Helpful? 0
  • +
  • -

#6 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: getting functions to play together.

Posted 10 October 2011 - 05:07 PM

You can put calls to functions inside of other function calls or you store the value in a variable and just pass that to the other function. For example...


# Store the value returned by inputMeters()
distMeters = inputMeters()

# Call another function and give it the value
km = kilometers(distMeters)




And the example of putting one call in another...

km = kilometers(inputMeters())



Here inputMeters is called, the user enters the value, it returns distMeters which is then immediately passed to the function kilometers.

:)
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1