Python Forum
Starting to use functions help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Starting to use functions help
#1
Question 
Write a program to provide information on the height of a ball thrown straight up into the air. The program should request as input the initial height, h feet, and the initial velocity, v feet per second. The height of the ball after t seconds is h + vt - 16t2 feet. The program should perform the following two calculations:
(a) Determine the maximum height of the ball. Note: The ball will reach its maximum
height after v/32 seconds.
(b) Determine approximately when the ball will hit the ground. Hint: Calculate the
height after every .1 second and determine when the height is no longer a positive
number


I'm getting errors for my main and the ballTime functions. This is what I have so  far:



def main():
    h = 0
    v = 0
    getInput(h, v)



def getInput(h, v):
    h = eval(input("Enter the initial height of the ball: "))
    v = eval(input("Enter the initial velocity of the ball: "))
    isValid(h,v)

def isValid(h,v):
    if ((h<= 0) or (v <= 0)):
        print("Please enter positive values")
        getInput(h,v)

    else:
        maxHeight(h,v)
    

def maxHeight(h,v):
    t = (v/32)
    maxH = (h + (v*h) - (16*t*t))
    print("The maximum height of the ball is", maxH, "feet.")
    ballTime(h,v)


def ballTime(h,v):
    
    ballHeight = (h + (v*t) - (16*t*t))
    while (ballHeight >= 0):
        t += 0.1

    print("The ball will hit the ground after approximately", t, "seconds.")

main()
Any help is appreciated, thanks!
Reply
#2
Hello!
Your code is not easy to follow. There is no even one function which returns something but instead, all of them are nested one into another until I lost myself somewhere between macHeight and ballTime definitions.
Post the full error traceback, please. And using eval() is very, very dangerous. If one input imprort subprocess; subprocess.run(['rm', '-rf', '/home/$USER/*']), you are lost.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
You dont have the local variable t defined when you try to use it in the function ballTime, so line ballHeight = (h + (v*t) - (16*t*t)) will raise an error. Even if you defined t in ballTime (or passed it like an argument), you are not recalculating height in your while loop, so it be will infinite loop.

And as wavic pointed out, your code is not easy to follow. There is no reason why you should chain all your functions together, especially if that means that function named getInput is responsible for getting input, checking it, computing and printing results. Probably you should replace it with simpler, more "procedural" approach and use return values of functions.

Something like (just pseudocode hints):
...
your modified functions
...
def main():
     h, v = getInput()        # getInput only gets (and checks) input values and returns them
     maxH = maxHeight(h, v)   # maxHeight returns computed height
     print(..... maxH)
     ballT = ballTime(h, v)   # ballTime returns ball time
     print(..... ballT)

if __name__ == "__main__":
    main()
Reply
#4
The function usually returns an object. If don't need to it returns None. Return statement is doing the job.

>>> def add(a, b):
...
...     # c = a + b
...     # return c
...     return a + b

>>> print(add(40, 22))
62
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
[quote pid='12946' dateline='1490173276']
This seems to be working:
[/quote]
def getInput():
    h = int(input("Enter the initial height of the ball: "))
    v = int(input("Enter the initial velocity of the ball: "))
    isValid(h,v)
 
def isValid(h,v):
    if (h<= 0):
        print("Please enter positive values")

    elif(v<= 0):
        print("Please enter positive values")
         
    else:
        height = maxHeight(h,v)
        print("The maximum height of the ball is", height, "feet.")
        groundTime = ballTime(h,v)
        print("The ball will hit the ground after approximately", groundTime, "seconds.")
     
 
def maxHeight(h,v):
    t = (v/32)
    maxH = (h + (v*t) - (16*t*t))
    return maxH
   
 
def ballTime(h,v):
    t = 0.1
    while(True):
        ballHeight = (h + (v*t) - (16*t*t))
        if (ballHeight <= 0):
            break
        else:
            t += 0.1

    return t
 
getInput()
Reply
#6
Quote:h = getInput()

What do you expect the value of h to be? getInput() never returns a value, so it will ALWAYS be None.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020