编写查找完美根的python代码
我写了一个程序来寻找一个数字的完美根,只要它在2到6之间,并且在没有答案时打印一条消息 我的问题是试图生成一个精确的print语句,当整数在2和6之间没有完美的根(比如400)时,它会打印完美的平方是20以及错误代码 这是密码编写查找完美根的python代码,python,Python,我写了一个程序来寻找一个数字的完美根,只要它在2到6之间,并且在没有答案时打印一条消息 我的问题是试图生成一个精确的print语句,当整数在2和6之间没有完美的根(比如400)时,它会打印完美的平方是20以及错误代码 这是密码 # Get an integer and output it's root and power such that root raised to the power equals the input integer and 0 < power < 6. #Ge
# Get an integer and output it's root and power such that root raised to the power equals the input integer and 0 < power < 6.
#Get an integer imput from the user
integer = int(input("Enter an integer "))
#check if integer is a perfect square
counter = 0
while counter ** 2 < abs(integer):
counter += 1
if counter ** 2 == abs(integer):
print("Square root of ", integer, " is", counter )
# cheacking if integer is a perfect cube
counter = 0
while counter ** 3 < abs(integer):
counter += 1
if counter ** 3 == abs(integer):
print("Cube root of ", integer, " is", counter)
#checking if integer is a perfect fourth root
counter = 0
while counter ** 4 < abs(integer):
counter += 1
if counter ** 4 == abs(integer):
print("fourth root of ", integer, " is", counter)
# checking if integer is a perfect fifth root
counter = 0
while counter ** 5 < abs(integer):
counter += 1
if counter ** 5 == abs(integer):
print("fifth root of ", integer, " is", counter)
# checking if integer is a perfect sixth root
counter = 0
while counter ** 6 < abs(integer):
counter += 1
if counter ** 6 == abs(integer):
print("sixth root of ", integer, " is", counter)
print("This integer has no perfext root")
# # A print statement for when the integers has no perfect such that 0 < pwr < 6
if counter ** 2 != abs(integer) and counter ** 3 != abs(integer) and counter ** 4 != abs(integer) and counter ** 5 != abs(integer) and counter ** 6 != abs(integer):
print("This integer has no perfext root")
#获取一个整数,并输出它的根和幂,这样提升到幂的根等于输入整数和0
如果没有完美的根,则使用if、elif和finally else,而不是使用just if语句。一种清除这种情况的方法是在幂上循环:对于范围(2,7)内的pwr:counter=0;当计数器**pwr…
时。顺便说一句,欢迎来到SO!查看和。这将有助于创建一个。此外,此行没有正确缩进:print(“此整数没有perfext根”)
感谢您的回答。。。。我发现你的答案非常有用,而不是只使用if语句,如果没有完美的根,使用if、elif和else。你可以通过在幂上循环来解决这个问题:对于范围(2,7)内的pwr:counter=0;当计数器**pwr…
时。顺便说一句,欢迎来到SO!查看和。这将有助于创建一个。此外,此行没有正确缩进:print(“此整数没有perfext根”)
感谢您的回答。。。。我觉得你的回答很有帮助