Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,659 Points

Last ditch question about a python assignment.

I am sorry this is so long. This is my last hope as I've tried everything. I am tasked to create a function that calculates the missing side of a right triangle. That side could be a, b or, c (which is the longest one, traditionally.)


Here is the assignment text:

Your task is to create a function that will solve for any side given two known sides. Specifics:

Your function should take three parameters: a floating point value for the first side, a floating point value for the second side, and a string to indicate the side to solve for. Your function should return a floating point value which holds the solved side.

For Example

a = 4.0

b = 2.0

c = pythag(a, b, 'c')

Notice the last parameter is a string which indicates the side to solve for. Suppose you want to solve for side a: b = 4.0 c = 2.0

a = pythag(b, c, 'a')

Required You must use the math.sqrt and math.pow functions to solve for the given side. What not to do • global variables • print in the pythag function • input in the pythag function

Your program should look like the following: (this is the requested output for full credit) Enter sides a and b and I will calculate C: 4.0 5.0 Side C is equal to: 6.4031242374328485

Enter sides b and c and I will calculate A: Side A is equal to: 3.3166247903554 5.0 6.0

Enter sides a and b and I will calculate B: Side B is equal to: 4.47213595499958 3.5 6.0


I am confused by the fact the user is asked for two parameters. Where is the third? We are instructed explicitly not to use global variables. There is no call to the user for the string to represent the missing side. This is the output as exactly as I can reproduce it.

I can make the output match the instructors when I just insert the numbers directly into the math.sqrt/math.power formulas. (Demonstrated below) I can also write a function but it only returns hypotenuses.

I am tempted at this point to believe there is something wrong with this assignment. But, maybe there is a trick I am missing. I am including my code below. If you can give me a hint as to what is wrong with my calculations or how I can make a third parameter without creating a global variable, I'l appreciate it.

#to prove I can make the math work
import math

ans3 = float(math.sqrt(math.pow(4, 2) + math.pow(5, 2)))
#is supposed to happen when user inputs 4 and 5

ans = float(math.sqrt(math.pow(6, 2) - math.pow(5, 2)))
#is supposed to happen when user inputs 5 and 6

ans2 = float(math.sqrt(math.pow(6, 2) - math.pow(4, 2)))
#is supposed to happen when user inputs 3.5 and 6


print(ans)
print(ans2)
print(ans3)

#the closest I've gotten in terms of a function.  Only makes one answer that matches the #task

# nmelucci571
# Assignment 12
# March 30 2017


import math


def pythagorama(x, y, z):
    ans = 0.0
    if type(z) is str:
        z = float(z)
        z = math.sqrt(math.pow(x, 2) + math.pow(y, 2))
        ans = z
    elif type(x) is str:
        x = float(x)
        x = math.sqrt(math.pow(z, 2) - math.pow(y, 2))
        ans = float(x)
    else:
        y = float(y)
        y = math.sqrt(math.pow(z, 2) - math.pow(x, 2))
        ans = float(y)

    return ans



print("This is the Pythagorian theorem")
print("Enter sides A and B and the side you wish to solve for and I will solve for it")
x = float(input())
y = float(input())

ans = pythagorama(x, y, 'z')
print("Side C is equal to " + str(ans))

print("Now enter sides B and C and I will calculate A")
y = float(input())
z = float(input())


ans = pythagorama(y, z, 'x')
print("Side A is equal to " + str(ans))


print("Now enter sides A and C and I will calculate B")
x = float(input())
z = float(input())


ans = pythagorama(x, z, 'y')
print("Side A is equal to " + str(ans))

3 Answers

I think the main issue is getting order of arguments right, which the instructions could be clearer about. You're interpreting the instructions as saying the arguments should be the 3 sides in order, with the 2 known sides as numbers and unknown side as a string. This would make the position of the string in the argument list tell the function which side is unknown. But the examples all have two numbers for the two known sides first and the string specifying the unknown side is always last. This would make the content of the string tell the function which side to solve for.

Nancy Melucci
Nancy Melucci
Courses Plus Student 36,659 Points

OK, I'll think about that, and try again tomorrow. It's due Sunday night. If I sleep on it maybe I can figure out how to tell the function what to do. I'll keep you posted. Thanks. Also for confirming my impression. Confusing.

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,659 Points

PS the call to the function does not appear on the console. Only in the instructions.

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 36,659 Points

I did finally solve it. Very painful process but I guess I win this round...

import math

def pythagorama(x, y, z):
    ans = 0
    if z is 'z':
        ans = math.sqrt(math.pow(x, 2) + math.pow(y, 2))
    elif z is 'x':
        ans = math.sqrt(math.pow(y, 2) - math.pow(x, 2))
    elif z is 'y':
        ans = math.sqrt(math.pow(y, 2) - math.pow(x, 2))
    return ans




print("This is the Pythagorian theorem")
print("Enter sides A and B and the side you wish to solve for and I will solve for it")
x = float(input())
y = float(input())

ans = pythagorama(x, y, 'z')
print(ans)

print("Now enter sides B and C and I will calculate A")
y = float(input())
z = float(input())

ans = pythagorama(y, z, 'x')
print(ans)

print("Now enter sides A and C and I will calculate B")
x = float(input())
z = float(input())

ans = pythagorama(x, z, 'y')
print(ans)