basic函数中一个简单python代码中的错误,它对接收到的数字起作用
我正在学习python,这是一个基本的代码,我不知道为什么它不工作。对于变量“index”我有错误“未使用的变量'index'”。对于命令,而我有一个错误“无法访问的代码”basic函数中一个简单python代码中的错误,它对接收到的数字起作用,python,loops,variables,while-loop,Python,Loops,Variables,While Loop,我正在学习python,这是一个基本的代码,我不知道为什么它不工作。对于变量“index”我有错误“未使用的变量'index'”。对于命令,而我有一个错误“无法访问的代码” 您的代码中没有错误。您得到的是编译器警告,而不是错误。目前,代码中的while循环永远不会执行。只需将while循环置于函数声明之外,即可消除警告 def power(base, pow): result = 1 for index in range(pow): result *= base
您的代码中没有错误。您得到的是编译器警告,而不是错误。目前,代码中的while循环永远不会执行。只需将while循环置于函数声明之外,即可消除警告
def power(base, pow):
result = 1
for index in range(pow):
result *= base
return result
while input("would you like to continue? ") == "yes":
print(power(input("enter the base: \n"), input("enter the power: \n")))
您的代码中没有错误。您得到的是编译器警告,而不是错误。目前,代码中的while循环永远不会执行。只需将while循环置于函数声明之外,即可消除警告
def power(base, pow):
result = 1
for index in range(pow):
result *= base
return result
while input("would you like to continue? ") == "yes":
print(power(input("enter the base: \n"), input("enter the power: \n")))
正如@markjacobson提到的,这些是IDE警告。您的代码仍将运行
def power(base, pow):
result = 1
for index in range(pow): # you set index to a value, but never use it
result *= base
return result # your function will exit here, so the code below will never run
while input("would you like to continue? ") == "yes":
print(power(input("enter the base: \n"), input("enter the power: \n")))
正如@Aniket在另一个答案中提到的,如果不需要while循环,或者不需要while循环,那么只需删除while循环即可,这样它就不在函数中了
警告不会影响程序,但如果要删除警告,请使用pow
进行迭代
def power(base, pow):
result = 1
while pow > 0:
result *= base
pow -= 1
return result
正如@markjacobson提到的,这些是IDE警告。您的代码仍将运行
def power(base, pow):
result = 1
for index in range(pow): # you set index to a value, but never use it
result *= base
return result # your function will exit here, so the code below will never run
while input("would you like to continue? ") == "yes":
print(power(input("enter the base: \n"), input("enter the power: \n")))
正如@Aniket在另一个答案中提到的,如果不需要while循环,或者不需要while循环,那么只需删除while循环即可,这样它就不在函数中了
警告不会影响程序,但如果要删除警告,请使用pow
进行迭代
def power(base, pow):
result = 1
while pow > 0:
result *= base
pow -= 1
return result
您的代码需要稍加修改:
def power(base, pow):
result = 1
for index in range(pow):
result *= base
return result
while input("would you like to continue? ") == "yes":
print(power(int(input("enter the base: \n")), int(input("enter the power: \n"))))
您的代码需要稍加修改:
def power(base, pow):
result = 1
for index in range(pow):
result *= base
return result
while input("would you like to continue? ") == "yes":
print(power(int(input("enter the base: \n")), int(input("enter the power: \n"))))
def功率(基本功率、功率):
结果=1
对于范围内的索引(pow):
结果*=基数
返回结果
while(输入(“您想继续吗?”)==“是”):
打印(power(输入(“输入基数:\n”)),int(输入(“输入功率:\n”)))
def功率(基本功率、功率):
结果=1
对于范围内的索引(pow):
结果*=基数
返回结果
while(输入(“您想继续吗?”)==“是”):
打印(power(输入(“输入基数:\n”)),int(输入(“输入功率:\n”)))
那些看起来像IDE警告,而不是实际的代码错误那些看起来像IDE警告,而不是实际的代码错误任何事情都没有改变@Surya LohiaHi@Surya LohiaHi在那里,提供一些解释通常是有用的,尤其是你的答案和问题本身之间的差异如此之小,我将while循环带到函数级别来传递值。类型转换是最重要的,因为我们知道我们不能以字符串的形式进行任何计算。嗨,在这里,提供一些解释通常是有用的,尤其是在你的答案和问题本身之间的差异如此之小的情况下。我已经将while循环带到函数级别来传递值。类型转换是最重要的,因为我们知道我们不能以字符串的形式进行任何计算。。