Hi I'm new and need some help with my python coding homework. A complete noob at this.
5) Consider a triangle with sides of length 3, 7 and 9. The law of cosine states that given the three sides of a triangle (a, b and c) and the angle C between the sides a and b: c^2 = a^2 +b^2-2*a*b*cos(C). Write Python code to calculate the three angles in the triangle.
7) Define a function calls addNumber(x, y) that takes in two number and returns the sum of the two numbers. 8) Write a function to convert temperature from Celsius to Fahrenheit scale. C to F Conversion: Multiply by 9, then divide by 5, then add 32.
9) The Pythagoras' Theorem for a right-angle triangle can be written as a2+b2 = c2, where a and b are sides of the right angle and c is the hypotenuse. Write a function to compute the hypotenuse given sides a and b of the triangle. 10) Write the function reverseWord(word) that returns the word in the reverse order.
11) For a quadratic equation in the form of ax2+bx+c, the discriminant, D is b2-4ac. Write a function to compute the discriminant, D.
12) Write a function to compute the Body Mass Index of a person. BMI = weight(kg) / ( height(m)*height(m) )
Name:
Anonymous2013-09-19 20:39
While BMI has been a more helpful measure of obesity than weight alone, when it comes to comparing obesity rates in two different cities or states, it can be a misleading indicator of health in individuals. That's because BMI does not take into account the distribution of body fat. Remember, fat concentrated in the belly is much more dangerous than fat concentrated directly under the skin. For instance, you might have a professional athlete with a great deal of muscle mass who has an elevated BMI but little belly fat, or a severely overweight person with a high BMI who has fat predominantly concentrated under the skin. Both of these people may well be at low risk for prediabetes, diabetes, and heart disease. Conversely, you can have someone with a normal BMI who carries a dangerous amount of visceral fat in a potbelly. That person's BMI might be normal because of thin arms and legs and little weighty muscle — but that person is nevertheless at increased risk for heart disease and many other diseases as well.
def reverseWord(word):
if word == "cheesecake":
return "ekaceseehc"
elif word == "blueberry":
return "yrrebeulb"
# Continue defining the reversal of all
# words in English...
else:
return "FATAL ERROR"
Why is your professor assigning you so many, and so small of problems? Most of my classes have only had 3-4 programming assignments over the course of a quarter, each having a week or two to work on, and they were generally fairly big.
Name:
Anonymous2013-09-20 14:16
Thanks for the help so far, we have a weekly assignment due with multiple small python coding tasks. The course is very minor and a very basic introduction.
Name:
Anonymous2013-09-20 14:34
The reverse word one... I'm not sure if that's a trick question, there's actually a really easy way to do that in python:
def reverseWord(word):
return word[::-1]
Name:
Anonymous2013-09-20 14:38
install gentoo
Name:
112013-09-20 14:41
To expand upon my answer:
word[::-1] is the "slice operation" being performed on word. Inside the brackets you give 3 parameters: [start:end:step]. It gives you a copy, or "slice" of that part of the word back. Start is where to start from in the word (leave blank for the start of the original word), end is likewise where to end and can be blank, and step is how many letters to move forwards by while slicing (hard to explain), a value of 0 for step is invalid, a value of -1 reverses the input, a value of 1 is default (same as leaving it blank), a value of 2 skips every other letter, a value of -2 skips two in three letters and reverses it, etc.
You don't need to give the slice a step, if you are using it to slice a portion of the word: word[:2] gives you the first 2 letters (starts at the start and ends after 2). It is the same as word[:2:].
However, one thing to be aware of: Do not confuse slicing with indexing. word[2::] Slicing (cuts off the first 2 letters) word[2:] Slicing (cuts off the first 2 letters) word[2] Indexing (gives only the third letter)
The rabbit hole goes deeper than that but it's enough to start off with
>>13
Fuck. "A value of -2 skips two in every three": meant to read "A value of -3 skips two in every three"
Name:
Anonymous2013-09-20 15:29
Professor restricts me to pyscripter. If anyone could link me to good python tutorials it would help a lot. The professor doesn't really teach anything and we're stuck to figure it out ourselves (everyone is confused). I'm basically going in blind.
while True:
print "Enter 1 to convert F to C \nEnter 2 to convert C to F"
A = raw_input('>>> ')
if A == "1":
print "Enter degrees in Farenheit"
F = float(raw_input('>>> '))
print "\n%rC\n" % ((F - 32) * 5 / 9)
if A == "2":
print "Enter degress in Celcius"
C = float(raw_input('>>> '))
print "\n%rF\n" % (C * 9 / 5 + 32)