首页 > 解决方案 > 值错误: int() 的无效文字无效文字,基数为 10:3,5

问题描述

这应该是我的输出
输入您的选择:3
输入底数,后跟指数:5 3 5
的幂次方为 3 是 125。

n,exp=int(input("Enter the value of the base followed by the exponent:")).split(",")
  def power(n,exp):
     if(exp==1):
        return(n)
     if(exp!=1):
       return(n*power(n,exp-1))
print("The power of",n, "raised to",exp,"is",power(n,exp))

错误

标签: python

解决方案


您无法转换5, 3为 int。首先将其拆分并将结果转换为 int

n, exp = [int(x) for x in input("Enter the value of the base followed by the exponent:").split(",")]

您还应该避免对全局和函数使用相同的变量名称。


推荐阅读