Horuson 2022-07-03 18:00 采纳率: 100%
浏览 22
已结题

python中exhuastive enumeration的练习疑问

问题遇到的现象和发生背景

要求用户输入一个正整数,运行完代码后可以得到两个正整数root和pwr,使得root**pwr等于该正整数。若没有这样的root和pwr,则应打印消息反映该情况

问题相关代码,请勿粘贴截图

user = int(input("enter a integer =! 1 or 0:"))
noFound = True

for pwr in range(2,6):
for root in range(2, abs(user+1)):
if rootpwr >= abs(user):(粗体rootpwr之间显示不出来的是两个乘号)
break
if root
pwr == abs(user):
noFound == False
break

if noFound == True:
print("There is no perfect root and pwr for your input!")
else:
print("The root is",root,"and the pwr is",pwr)

运行结果及报错内容

无论输入什么数值,最后都显示“There is no perfect root and pwr for your input!”

我的解答思路和尝试过的方法

另外的解答方法如下
root = 0
while root <= abs(user):
for pwr in range(0,6):
if root**pwr == abs(user):
if user >= 0:
print("The root is",root,"and the pwr is",pwr)
elif user < 0 and pwr%2 == 1:
root = -root
print("The root is",root,"and the pwr is",pwr)
else:
print("There is no root and pwr for your input!")
break
else:
root += 1

if root > abs(user):
print("There is no perfect root for your input!")

我想要达到的结果

对于任意输入的正整数(先简化来),都能得到其开方根和相应的指数

  • 写回答

2条回答 默认 最新

  • 天际的海浪 2022-07-03 18:20
    关注

    代码1中 noFound == False 两个==是比较
    要改成 noFound = False 一个=才是赋值

    user = int(input("enter a integer =! 1 or 0:"))
    noFound = True
    for pwr in range(2,6):
        for root in range(2, abs(user+1)):
            if root**pwr >= abs(user):
                break
        if root**pwr == abs(user):
            noFound = False   # == 改成 =
            break
    
    if noFound == True:
        print("There is no perfect root and pwr for your input!")
    else:
        print("The root is",root,"and the pwr is",pwr)
    

    如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月11日
  • 已采纳回答 7月3日
  • 修改了问题 7月3日
  • 修改了问题 7月3日
  • 展开全部

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!