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日
  • 展开全部

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度