-3

I have the following code:

import math
q = input("Is there a square root in the function? (y,n) ")
if q == "y":
  base = input("base? ")
  x_value = input("x? ")
  print (math.log(math.sqrt(base),x_value))
else:
  base_2 = input("base? ")
  x_value_2 = input("x? ")
  print (math.log(base_2, x_value_2))

When I run the code, it says that the second value in math.log() must be a number. Shouldn't it work if I just use the variable I assigned to it?

Noah Brown
  • 123
  • 1
  • 3
  • 8

1 Answers1

1

input() returns a string. You should convert the user inputs to floating numbers with the float() constructor:

import math
q = input("Is there a square root in the function? (y,n) ")
if q == "y":
  base = float(input("base? "))
  x_value = float(input("x? "))
  print (math.log(math.sqrt(base),x_value))
else:
  base_2 = float(input("base? "))
  x_value_2 = float(input("x? "))
  print (math.log(base_2, x_value_2))
blhsing
  • 91,368
  • 6
  • 71
  • 106