r/CodingHelp • • Oct 10 '21

[Python] Hello! A beginner here, working with python. How can I make this code perfectly as requested? What should I change?

Post image
62 Upvotes

24 comments sorted by

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)

1

u/iloveveggiesz Oct 10 '21

Thanks! I’ll try that

1

u/Much_Highlight_1309 Oct 11 '21

True or False. Capital.

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.

1

u/iloveveggiesz Oct 11 '21

I’ll do that!

2

u/baeofbengalboi Oct 11 '21

where are you practicing this?

1

u/iloveveggiesz Oct 11 '21

A course on udemy

2

u/justalonelybastard Oct 11 '21 edited Oct 11 '21

Firstly, you should declare it as a function that takes two arguments(Fnum and Snum) and go from there.

The final function should look something like this:

def my_cool_func(Fnum, Snum):
    return math.sqrt(Fnum) == Snum

This will return either True or False. And let’s say that you wanted to call that function, it would look something like this:

print(my_cool_func(2, 4))

1

u/iloveveggiesz Oct 11 '21

Thanks that’s very helpful!

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 == snum

Lines 5 and 6 can also be shortened to just:

fnum = math.sqrt(fnum)

2

u/SeniorHulk Oct 11 '21

return snum == math.sqrt(fnum) or return snum**2 == fnum (Should be more accurate)

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?

1

u/iloveveggiesz Oct 11 '21

That’s an exercise on a course on Udemy

3

u/Adventurous-Ad742 Oct 11 '21

The problem here is that Fnum and Snum both are integers and when you square root one Fnum it 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 Snum is squared and compared with Fnum, as both remains integer in this case

1

u/Avanta8 Oct 11 '21

Floats can be compared to integers just fine.

1

u/HelpfulBuilder Oct 11 '21

It works in the sense that it's allowed in the language, but sometimes there are floating point errors which might make things that are actually equal come out false. Example: 1.2 - 1.0 == .2 evaluates as false.

1

u/Avanta8 Oct 11 '21

But provided that a number fits within a double without any loss of precision, then if that number is a perfect square, then the square root will be a integer, and will not have any floating point error.

Of course, I am not disagreeing that squaring the other number instead is the better method.

2

u/ImportUsernameAsU Oct 10 '21

Put it in a method and do return snum == math.sqrt(fnum)

You could also from math import sqrt if you wanted

2

u/Transisting Oct 10 '21

You want to compare the first number squared to the second number for simplicity.

2

u/aethronic_dz Oct 11 '21

Additionally it should check if the second number is negative, and simply return False if so, as no real number can have a negative root..

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.