This isn't really a code bug but a math functions inside python problem. I know my code isn't great and messy, but i'm not sure how to display the law of cosines math in python. As you see I try to do it but I am always roughly a few integers off the answer. If you needa reminder the law of cosines is a^2= b^2 + c^2 - 2bcCos(A)
#๐ Attempting to Make a Law of Cosines Calculator
84 messages ยท Page 1 of 1 (latest)
@rich schooner
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
dont ask abou the mesiness of the code lol it's bad i know i js am confused about how to print the actual math the law of cosines does.
Dunno about the law, but why do you convert float to int?
dont ask about the messiness pls ๐ญ
ik theres a crap ton of things i can improve
I was actually asking if there is a logical reasoning
nope im js tred
tired
brain no work
floats and ints are quite different kind of numbers, and you'll get different results
Well, you're off from your answer by a few, so maybe not convert to int
wont float just allow decimals or smth (sorry im new to python)
k
ah
You're losing information when converting to int
>>> 3.5 * 2
7.0
>>> int(3.5) * 2
6
Ints get rid of the decimal part
what is unresolved refernce?
im getting that on these @dense blaze
Could you show the entire code
Interesting
idk why its ;like that
Can you try running it
Ah
Alright
you should try to print the intermediate values, to see where they go wrong
-2 is missing from your formula right?
yeah cause now i fixed smth and its off by .7 ish
fixed that
I can see you're using a variable called part 2
now its off around 0.1 -2 every time
yes
dont question the part
lmao
So its still off?
yes but by very little
so i think its rounding issue?
nvm
off by 50 lol
hewyvihoe
math.cos takes radians, maybe that?
Oh i see
ohh how would i make that degrees?
Yeah you should pass math.radians(160) to math.cos()
>>> 43**2 + 53**2 - (2*43*53*math.cos(math.radians(160)))
8941.11896554217
``` at least now the number matches
So you'd do math.cos(math.radians(160))
On line 16
Of course replace 160 with the actual variable name
Think so yeah
btw, your code isn't messy at all. You don't have to worry about that. ๐
holy crap it works
Yeah its normal code
thanks im a beginner but i always try to optimize it when i can
like i rmemeber i didnt need to use a variable to square a number
there is very little you can do wrong in 15 lines, unless you're part of #esoteric-python ๐
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.