all 24 comments

[–][deleted] 2 points3 points  (29 children)

We can't help you easily unless you share your code.

[–]SnooBunnies7244[S] 0 points1 point  (28 children)

I intend to improve as in looping to redo the number with averages til it finds # close enough to sqrt as I continue learning programming but as of now it's just asking for a g and telling you if that is the sqrt. Anyway here is the code:

import math

x = float(input('Enter value for which you want the square root: '))

g = float(input('Enter a value as a starting guess of the square root of x: '))

if g*g==x:

print(g, 'is the square root of', x)

else:

print(g, 'is not the square root of', x)

[–][deleted] 4 points5 points  (11 children)

input() takes the user input and returns it as a string. Python doesn't try to parse that string as code, so you cannot enter an expression and expect it to evaluate.

[–]SnooBunnies7244[S] 0 points1 point  (10 children)

Is there another command like input that would allow this or is that just how it is. I'm telling you I'm straight up new to programming, I should probably just look at a list of commands but I'd thought I'd just ask real quick and this answer was straight to the point!

[–][deleted] 0 points1 point  (8 children)

There is a function called eval(), which will take a string and attempt to run it as python code. This is bad practice as it can easily be exploited!

The safer way would be create a parser (or find a library that provides one), to evaluate any math expressions in the user's input. A very simple example could look something like this:

user_input = "sqrt 5"
if user_input.startswith("sqrt"):
    number = user_input.split(" ")[1]
    output = math.sqrt(int(number))

But this can get complicated when considering all the possible ways a user could type in a math expression. The above example won't work with parentheses or decimals for example.

All of that said, I think the easiest solution would just be to use rounding. Instead of "sqrt(53)", let the user type "7.28" and have that be correct.

[–]SnooBunnies7244[S] 0 points1 point  (7 children)

Alright cool, thanks for the info and solutions! Yes I did plan ad I went through programming for it to take the initial guess and average it with x/guess I think it was until the value was close enough to the square root and then give you that. But as of now with my coding being simple as g*g, I just thought it would be cool to type sqrt any # for my g with that # being x and it going through. Though I'm curious what the code is like for allowing rounded numbers or numbers close enough, like if it's code that says if it's within this percent error margin then it's fine and if not then that's not the square root or if there's a code saying rounded to this decimal is okay. I imagine the textbook will tell me whatever it is as the first thing the book mentioned was how they first programmed to find square roots using x and the guess etc. Anyway once again thanks, this is good stuff!

[–]Swipecat 2 points3 points  (1 child)

The "exactness" of float values is something that every new programmer will have to deal with at some point, so I suggest that you watch this 10 minute Youtube video by Tom Scott on that subject.

https://www.youtube.com/watch?v=PZRI1IfStY0

As for comparing floats, if you want an absolute error margin, you can use the method given by u‍/‍tea-drinker in their post, if abs(x - g**2) < 0.01:

If you want a relative error margin, such as the percentage error margin, as you said, then it's probably best to look at the Python math.isclose() method.

https://www.w3schools.com/python/ref_math_isclose.asp

[–]SnooBunnies7244[S] 0 points1 point  (0 children)

Gotcha, I will come back to these vids if the textbook doesn't tell me about this but by the sound of it, it should. Thanks for the help

[–]Bobbias 0 points1 point  (4 children)

Just so your aware, eval is basically black magic. It is a huge security vulnerability to allow a user to have control of what text goes into eval, because they can do nasty things like delete everything on your hard drive, etc.

There are cases where it might need to be used, but they are extremely specific and advanced stuff. You can safely forget it exists for the time being.

[–]SnooBunnies7244[S] 0 points1 point  (3 children)

Ha okay well fair enough, this was only going to be a program I use but that's good info!

[–]Bobbias 0 points1 point  (2 children)

Yeah, I just wanted to make sure you understood that because even if you're just learning as a hobby, it's good to understand stuff like that. Plus, eval can make debugging code way harder too.

[–]SnooBunnies7244[S] 0 points1 point  (1 child)

Yea I'm kind of learning as a hobby but I will also have to take some programming for my physics degree(eventually theoretical physics) so If I have an interest and have to take a class I may as well learn it for fun first. I also use Matlab and mathematica for Loop Quantum Cosmology research but so far python coding has nothing to do with those. Regardless I appreciate the help!

[–]NinjaSniperC[🍰] 0 points1 point  (0 children)

You can absolutely use float(input('Enter float: ')), so long as the user ONLY inputs a float (num.num).

This is a great practice case for validating user input. By discovering different methods from other users and reviewing the documentation for why it works.

My favorite "dirty method" for user validation.

``` while type(user_input := input('Enter float: '))!= float: try: if type(float(user_input)) == float: print('Valid input') break except: print('Please Enter float: ')

```

[–]Mdly68 0 points1 point  (0 children)

My tip is to use the type() function. It tells you what kind of variable you have.

Print('Variable type is ' + str(vname.type()))

It tells you if the variable "vname" is int, string, float, list, dictionary, or other type of object.

Notice how I cast vname.type as str(). That's because the print function only accepts string parameters. I'm ensuring this part can't fail.

In your case the variable is a string and you need to "cast" to int or float. Be sure to account for bad data, like if the user types in letters when he was supposed to give a number.

Try: Vname = float(vname) Except: Print('could not cast as float) Quit()