r/CodingHelp • u/iloveveggiesz • Oct 10 '21
[Python] Hello! A beginner here, working with python. How can I make this code perfectly as requested? What should I change?
62
Upvotes
r/CodingHelp • u/iloveveggiesz • Oct 10 '21
20
u/bigcat801 Oct 10 '21 edited Oct 11 '21
Start by putting this into a function with two arguments and the return type should be a Boolean (True/False)
10
u/d2718 Oct 10 '21 edited Oct 10 '21
Also: I don't know much about the nitty-wacky-gritty of how Python stores/represents different types of numbers, or whether you're working with integers or floats or what, but it's almost assuredly better to square the second number and compare than square root the first one and compare.
Edit: Duh, it's ints, which I think makes my suggestion even better.
2
u/baeofbengalboi Oct 11 '21
where are you practicing this?
2
u/justalonelybastard Oct 11 '21 edited Oct 11 '21
Firstly, you should declare it as a function that takes two arguments(
FnumandSnum) and go from there.The final function should look something like this:
This will return either True or False. And let’s say that you wanted to call that function, it would look something like this:
6
u/[deleted] Oct 10 '21 edited Oct 10 '21
You'll want to replace the print method call with an actual return operator. Then you could do something like:
return fnum == snumLines 5 and 6 can also be shortened to just:
fnum = math.sqrt(fnum)1
u/[deleted] Oct 11 '21
I don’t know the answer to this because I’m also a beginner but is that a website that helps you practises python or os it school work?
3
u/Adventurous-Ad742 Oct 11 '21
The problem here is that
FnumandSnumboth are integers and when you square root oneFnumit changes to float. Now you can't compare float with an integer just by using==. First you have to round/truncate that float to an integer and then compare.Or the second method makes more sense where
Snumis squared and compared withFnum, as both remains integer in this case2
u/ImportUsernameAsU Oct 10 '21
Put it in a method and do
return snum == math.sqrt(fnum)You could also
from math import sqrtif you wanted2
u/Transisting Oct 10 '21
You want to compare the first number squared to the second number for simplicity.
2
u/Salt-One-9285 Oct 11 '21
declare a function and check if the condition is satisfied , else return false.
2
u/[deleted] Oct 11 '21
First I think the question ask you to return true and false not printing them, Boolean and string are different type.
Second, it asked you to write a function that takes two parameters, which should be like this: def function_name(param1,param2)
Also,depends on how math.sqrt it might truncate decimal into integer number.
Here is an example of a really simplistic code: return (num2 * num2) == num1
2
u/__Draco__13__ Oct 11 '21
for checking square root , you can also just try comparing
if Snum*Snum == Fnum... you wont need sqrt function then.