gpt4 book ai didi

python - 这个python程序有什么错误?

转载 作者:太空宇宙 更新时间:2023-11-04 10:23:58 25 4
gpt4 key购买 nike

我最近刚开始学习用 python 编写代码。我正在尝试编写一个程序,其中给定的整数将给出具有根和幂的输出(例如范围设置为 5)。这是代码:

user_input = int (raw_input ('Enter your number: '))
root = 1
pwr = 1
def noint ():
return 'no intergers were found'

def output ():
return root, '^', pwr
if user_input < 1:
print noint ()

elif user_input == 1:
print output ()

else:
while root < user_input:
root = root + 1
if root == user_input:
print output()
else:
for power in range(5):
if root ** power == user_input:
pwr = power
print output()

现在,如果我尝试将 25 作为输入,则输出为:(5, '^', 2)(25, '^', 2)

但是如果我尝试任何质数,比如 7,输出是:

(7, '^', 1)

给我额外输出 (25, '^', 2) 的编码有什么问题?

最佳答案

你这样做:

while root < user_input:
root = root + 1
if root == user_input:
print output()

也就是说,对于root == 24你仍然进入循环,将它增加到25,然后你打印它,因为root == user_input.

关于python - 这个python程序有什么错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30598398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com