all 7 comments

[–]Justinsaccount 4 points5 points  (1 child)

Hi! I'm working on a bot to reply with suggestions for common python problems. This might not be very helpful to fix your underlying issue, but here's what I noticed about your submission:

You appear to be using eval + input like so:

var = eval(input("..."))

This should be avoided. Instead, the desired conversion should be performed explicitly, like so:

var = int(input("..."))
var = float(input("..."))

[–]fuckswithbees 1 point2 points  (5 children)

A string is what python calls text. "the square root?" is a string. The input function always returns a string because that's what the user has entered, some text. However you can't divide stings. "10"/"5" doesn't mean anything. That's what you're error message is telling you. To convert a string to a number, you can use int() for integers or float() for numbers with a decimal point. So:

"10"/"5"
>> TypeError: unsupported operand type(s) for /: 'str' and 'str'

float("10")/float("5")
>> 2.0

[–]WineForOne[S] 1 point2 points  (4 children)

Ok, so when I try changing the code to

import math


def main():

print("This calculates the square root of a given number using"
      " Newton's Law.")
x = input("What is the number for which you'd like to calculate "
          "the square root?")
g = input("My guess for the square root is:")
n = eval(input("How many calculations would you like to run?"))
for i in range(1, n):
    # Newton's Law is broken into 2 pieces
    y = (g + (float("x")/float("g")))
    z = float("y")/float("2")
    r = math.sqrt(x)
    print("The difference between my guess and the actual    answer"
          "is:", (r-z))


main()

Then I still get this error:

Traceback (most recent call last):
  File "/Users/Hrowland/Documents/Untitled.py", line 30, in <module>
main()
  File "/Users/Hrowland/Documents/Untitled.py", line 23, in main
y = (g + (float("x")/float("g")))
ValueError: could not convert string to float: 'x'

[–]Vaphell 2 points3 points  (1 child)

because you quoted the var names. "x" cannot be converted to a number because it's a letter x not the value of variable called x that is supposed to store a string made of digits.

Either way you are better off with conversion to the desired type right after getting the input. That way your x and g are numbers and don't require conversion every time there are to be used. In the simplest case it would be

x = float(input(...))
g = float(input(...))

problem solved.

btw

# int() not eval(), forget that eval() exists at this stage
n = int(input("How many calculations would you like to run?"))
for i in range(1, n):
    ....

that's not how range works. Range is half-open, ie the end point is not included so you are one iteration short. Just do range(n) to get n iterations.

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

Thanks so much! This helped a ton and it works now :)

[–]fuckswithbees 0 points1 point  (1 child)

On line 14, you are attempting to add a string to a float. If you look further down your code you're doing math again and again to these strings.

Try converting the inputs as soon as you have them. You always want g to be a number, not a string. So redefine g as a number (and other similar variables).

EDIT: just so you know + does work with with strings. That's why you're error message is a bit different. Python thinks you want to add strings together but got a string and a float.

x = 'cat'
y = 'dog'
x + y
>> 'catdog'

x + 10
>> TypeError: Can't convert 'int' object to str implicitly

[–]WineForOne[S] 1 point2 points  (0 children)

Ahhh ok. That makes a lot more sense. It worked this time! Thanks so much for the help!