Help


#1



What is wrong here?


import math
def pythagorean_theorem():
    hypotenuse = raw_input("side c or hypotenuse please?")
    side_a = raw_input("side a please?")
    side_b = raw_input("side b please?")
    sides = [side_b, side_a, hypotenuse]
    for i in sides:
        if int(i) != i:
            break                    
        else:
            loser = i**2
            goat = i**2
            

    answer = math.sqrt(loser - goat)
    print 'x = '+ answer


#2

When all three sides are known, what are we setting out to find?

Known                  Unknown
side a, side b         hypotenuse
side a, hypotenuse     side b
side b, hypotenuse     side a

This suggests that the user desribe the two knowns entered, and the program will find the missing third side.

    prompt user for a side, a, b or c.

    if side is a, prompt user for a side, b or c

    else if side is b, prompt user for a side, a or c

    else if side is c, prompt user for a side a or b

That’s a lot of prompting which suggests a single function for that purpose,

def get_sides(side=''):  # when no argument is given this is the default
    if side == '':
        sides = 'a, b or c'
    elif side = 'a':
        sides = 'b or c'
    elif side = 'b':
        sides = 'a or c'
    elif side = 'c':
        sides = 'a or b'
    return raw_input("Choose a side, %s" % sides)

You’ll see how this works when we start to use it. The return value is a string, which at present is assumed to be one character, a, b, or c.

We do know there are two other functions that we can use:

from math import sqrt

def get_hypotenuse(a, b):
    return math.sqrt(a ** 2 + b ** 2)

def get_side(k, h):
    return math.sqrt(h ** 2 - k ** 2)

Now we just need some logic and a data structure to pull it all together. This is probably getting deeper than you had planned or expected. It is not always simple, and takes some planning to break things down to manageable bite sizes. We have above three useful and re-usable code blocks, so let’s get to work with the rest.

sides = {}

# get descriptors for the known sides

s_1 = get_sides()
s_2 = get_sides(s_1)  # this excludes s_1 from the next prompt

# get data

sides[s_1] = float(raw_input("Enter a value for side %s: " % s_1))
sides[s_2] = float(raw_input("Enter a value for side %s: " % s_2))

We now have a dictionary with two keys, the letter stored as s_1 is one, and the letter stored as s_2 is the other. To determine the correct course of action, we simply look for the one that is not there. The unknown side.

The data is stored as float for numeric precision. We can round and fix digits as required, but for now I’ve allowed full floats.

keys = sides.keys()

if 'c' not in keys:
    sides['c'] = get_hypotenuse(sides['a'], sides['b'])
    side = 'c'
elif 'b' not in keys:
    sides['b'] = get_side(sides['a'], sides['c'])
    side = 'b'
else:
    sides['a'] = get_side(sides['b'], sides['c'])
    side = 'a'

At this point, side is storing the unknown side descriptor. Now we have only to report our findings.

print "Given side %s = %r, and side %s = %r,\n \
the measure of the unknown side, %s, is %r" \
% (s_1, sides[s_1], s_2, sides[s_2], side, sides[side])

Now we take it for a spin…

Choose a side, a, b or c:  a
Choose a side, b or c:  c
Enter a value for side a:  8
Enter a value for side c:  25
Given side a = 8.0, and side c = 25.0,
 the measure of the unknown side, b, is 23.685438564654021

This is by no means complete, but given honest inputs, it gives honest results. The real work ahead is validating the inputs so no garbage gets into the final logic.

# pythagorean triangle calculator by Roy

from math import sqrt

def get_sides(side=''):
    if side == '':
        sides = 'a, b or c'
    elif side == 'a':
        sides = 'b or c'
    elif side == 'b':
        sides = 'a or c'
    elif side == 'c':
        sides = 'a or b'
    return raw_input("Choose a side, %s: " % sides)

def get_hypotenuse(a, b):
    return sqrt(a ** 2 + b ** 2)

def get_side(k, h):
    return sqrt(h ** 2 - k ** 2)
'''
print get_side()
print get_side('a')
print get_side('b')
print get_side('c')
'''
s_1 = get_sides()
s_2 = get_sides(s_1)
#print s_1, s_2
sides = {}
sides[s_1] = float(raw_input("Enter a value for side %s: " % s_1))
sides[s_2] = float(raw_input("Enter a value for side %s: " % s_2))
#print sides
keys = sides.keys()
#print keys

if 'c' not in keys:
    sides['c'] = get_hypotenuse(sides['a'], sides['b'])
    side = 'c'
elif 'b' not in keys:
    sides['b'] = get_side(sides['a'], sides['c'])
    side = 'b'
else:
    sides['a'] = get_side(sides['b'], sides['c'])
    side = 'a'

#print sides

print "Given side %s = %r, and side %s = %r,\n \
the measure of the unknown side, %s, is %r" \
% (s_1, sides[s_1], s_2, sides[s_2], side, sides[side])

#3

And why stop there?

def get_sides(side='d'):
    sides = {
        'a': 'b or c',
        'b': 'a or c',
        'c': 'a or b',
        'd': 'a, b or c'
    }[side]
    return raw_input("Choose a side, %s: " % sides)

and,

def get_data(side):
    return float(raw_input("Enter a value for side %s: " % side)) 

and,

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

These steps simplify the code considerably.

sides[s_1] = get_data(s_1)
sides[s_2] = get_data(s_2)

if c not in sides.keys():
    sides[c] = get_hypotenuse(sides[a], sides[b])
    side = c
elif b not in sides.keys():
    sides[b] = get_side(sides[a], sides[c])
    side = b
else:
    sides[a] = get_side(sides[b], sides[c])
    side = a

The work continues as we narrow this down to a tight program. More refactoring will come, assuredly, but not before we sort out the validation. Are you up for it?

Code
# pythagorean triangle calculator by Roy

from math import sqrt

def get_sides(side='d'):
    sides = {
        'a': 'b or c',
        'b': 'a or c',
        'c': 'a or b',
        'd': 'a, b or c'
    }[side]
    return raw_input("Choose a side, %s: " % sides)

def get_hypotenuse(a, b):
    return sqrt(a ** 2 + b ** 2)

def get_side(k, h):
    return sqrt(h ** 2 - k ** 2)
    
def get_data(side):
    return float(raw_input("Enter a value for side %s: " % side)) 

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

sides = {}

s_1 = get_sides()
s_2 = get_sides(s_1)

sides[s_1] = get_data(s_1)
sides[s_2] = get_data(s_2)

if c not in sides.keys():
    sides[c] = get_hypotenuse(sides[a], sides[b])
    side = c
elif b not in sides.keys():
    sides[b] = get_side(sides[a], sides[c])
    side = b
else:
    sides[a] = get_side(sides[b], sides[c])
    side = a

print "Given side %s = %r, and side %s = %r, \
\nthe measure of the unknown side, %s, is %r" \
% (s_1, sides[s_1], s_2, sides[s_2], side, sides[side])



#4

Quick question is this python 2 or 3???


#5

Look at the input statements. That will be your first clue.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.