chevron_left chevron_right

• 0 Vote(s) - 0 Average

 Slope Calculator filter_list Linear Mode Threaded Mode View a Printable Version Author Message
Slope Calculator #1
Formula for slope:
y2 - y1
______

x2 - x1

I made a simple calculator, but it took me so long because I missed a ")".

I know it's simple, but yeah

I will be making more stuff, I might send this to my math teacher for the lulz (we learned slope like a long time ago but yeah, we are doing something now that INVOLVES slope).
Code:
```x1 = int(input("Enter the first x value.")) x2 = int(input("Enter the second x value.")) y1 = int(input("Enter the first y value.")) y2 = int(input("Enter the second y value.")) #^Just a bunch of stuff, assigning whatever you enter to the variable above. print ("The slope is", y2 - y1, "/", x2 - x1); #^Does all the math and prints it out.```

RE: Slope Calculator #2
Gah, do validity checks, at the very least. D:

Avatars are for faggots.

RE: Slope Calculator #3
dafuq is a validity check

RE: Slope Calculator #4
Just makes sure that the entered value is an integer.
You directly converted the input to an integer, so any invalid values will result in an error.

I mean, yeah, this is simple as fuck, but sometimes that little extra step is all it needs to be a decent bit of code.

Avatars are for faggots.

RE: Slope Calculator #5
How would I do that?
Sorry, I am new to Python :l

RE: Slope Calculator #6
There are a few ways, but one of the easiest is try-except.

Code:
```x1 = input("Enter the first x value.") try: x1 = int(x1) except: x1 = 0 x2 = input("Enter the second x value.") try: x2 = int(x2) except: x2 = 0```
and so on.

Mind you I said this was one of the easiest, not one of the best. There are other, more suitable ways of doing it.

Avatars are for faggots.

RE: Slope Calculator #7
What does try do, and what does except do?

RE: Slope Calculator #8
(11-09-2013, 04:49 AM)Tear Wrote: What does try do, and what does except do?

People like you are why I'm depressed.

http://docs.python.org/2/tutorial/errors.html
PGP
Sign: F202 79C9 76F7 40BB 54EC 494F 5DEF 1D70 14C1 C4CC
Encrypt: A5B3 1B21 55E1 80AF 4C6E DE83 467B 8EFC 3DEE 681C
Auth: CD55 E8A5 1A08 2933 8BA6 BC88 D81F 1943 739A 3C47

RE: Slope Calculator #9
(11-09-2013, 04:54 AM)Kirika Yūmura Wrote: People like you are why I'm depressed.

http://docs.python.org/2/tutorial/errors.html

Please kill yourself, I recommend a drug overdose.

I use python 3.3

RE: Slope Calculator #10
It's kinda like if-else, but for errors:

Try is exactly what is sounds like, it tries to perform a bit of code.
Normally, when something causes an error, like trying to convert a string with letters to an integer, the script will cease to work and spit out an error.

If you use try-except, Except will catch that error. Usually that's used to display a more helpful error message, but it can also be used to very easily check if something is a valid instance.

Code:
```sampleString = "Hello!" print(int(sampleString)) print("Finished executing~!")```

The above code produces a TypeError, since "Hello!" is not a valid integer, and the script will never reach the second print. So to fix that, you would use try-except.

Code:
```sampleString = "Hello!" try:     print(int(sampleString)) except Exception as e:     print(e) print("Finished executing~!")```

The code will still print out the error, but now it continues on to the final print. You can also make it silent by changing print(e) to pass.

And just so you know, any try MUST have a matching except, even if you're not going to use it. An unused except must, at the very least, contain a pass statement.

@Kirika; Sometimes docs are a little hard to understand for the uninitiated, get the sand out of your vagina.

@Tear(previous post); The 2.x docs are largely the same as the 3.x docs, and even when they aren't, will usually link to the recommended version at the top. Also, there's a drop-down at the top of all doc pages that will move you to the valid page for your selected version.

Avatars are for faggots.

Users browsing this thread: 1 Guest(s)