说 X 必须是对数函数中的值?

Says X must be a value in logarithmic function?

我有以下代码:

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))

当我 运行 代码时,它说 math.log() 中的第二个值必须是一个数字。如果我只使用分配给它的变量,它不应该工作吗?

input() returns 一个字符串。您应该使用 float() 构造函数将用户输入转换为浮点数:

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))