Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Python Coding Homework

Name: Anonymous 2013-09-19 20:32

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: 11 2013-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

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List