这是从使用Python进行计算和编程的简介中的手指练习3.1开始的。Python 2中的嵌套while循环
编写一个程序,要求用户输入的整数,并打印两个整数,
root
和pwr
,使得0 < pwr < 6
和root**pwr
等于由所输入的输入的整数。如果不存在这样的整数对,它应该打印一条消息来达到这个效果。
我该死的关闭,部分原因是Stack Overflow中的一些建议。但是没有人能够一路走到那里,部分原因是练习的重点是使用嵌套的While循环,而不是For循环或其他任何比这更复杂的东西。这是一个详尽的枚举。
我真的很亲密。这是我到目前为止有:
num = int(raw_input('Enter a positive integer: '))
power = 0
root = 0
while power < 6:
if root ** power == num:
break
power += 1
root= 0
while root ** power < num:
root += 1
if root**power == num:
print('Root = ' + str(root) + ' and power = ' + str(power))
else:
print('No pair of integers exist such that one has an exponent that is between 1 and 6')
这里有只是两个问题:我要运行检查,看是否根和电源等于用户输入的那种感觉不必要的。想知道如何使这种清洁剂。
此外,当用户输入1
进行输入时,程序输出Root 0 and Power 0
,这是练习参数的外部。
'0
Leonardo