简体   繁体   English

使用Python进行计算和编程简介

[英]Intro to Computation and Programming Using Python

I am trying to solve Finger Exercise 3.1, and I can't figure out what I am doing wrong here. 我正在尝试解决手指练习3.1,我无法弄清楚我在这里做错了什么。 When I enter '1' as the integer, it returns 0 and 0. 当我输入'1'作为整数时,它返回0和0。

I am a complete newbie to programming and Stack Overflow, so I'm not sure if I am doing this correctly, but I figured I would give it a shot. 我是编程和Stack Overflow的完全新手,所以我不确定我是否正确这样做,但我想我会试一试。

This is the problem: Write a program that asks the user to enter an integer and prints two integers, root and pwr, such that 0 < pwr < 6 and root**pwr is equal to the integer entered by the user. 这就是问题:编写一个程序,要求用户输入一个整数并输出两个整数root和pwr,这样0 <pwr <6和root ** pwr等于用户输入的整数。 If no such pair of integers exists, it should print a message to that effect. 如果不存在这样的整数对,则应该打印一条消息。

And here is my solution thus far: 到目前为止,这是我的解决方案:

x = int(raw_input('Enter a positive integer: '))
root = 0
pwr = 0
while pwr < 6:
    pwr += 1
    while root**pwr < x:
        root += 1
if root**pwr == x:
    print "The root is " + str(root) + " and the power is " + str(pwr)
else:
    print "No such pair of integers exists."

How can I fix my code so that it returns the correct integers? 如何修复代码以使其返回正确的整数? What am I doing wrong here? 我在这做错了什么? What logic am I missing? 我错过了什么逻辑?

One problem is that, while you do have conditions that end your loops, they will always go all the way up to the maximum allowed condition. 一个问题是,虽然你确实有条件可以结束你的循环,但它们总会一直到最大允许条件。 You could solve that with break or, as shown, by using return in a function. 您可以使用break来解决这个问题,或者如图所示,通过在函数中使用return来解决。 Also, instead of using a counter, use the xrange() function ( range() in Python 3). 另外,不使用计数器,而是使用xrange()函数(Python 3中的range() )。

>>> def p(num):
...     for power in xrange(6):
...         for root in xrange(num/2+1):
...             if root**power==num:
...                 return root, power
...
>>> r, pwr = p(8)
>>> print 'The root is', r, 'and the power is', pwr
The root is 2 and the power is 3

Albeit not very idiomatic for Python, you are close to a correct answer. 虽然对Python来说不​​是很惯用,但你接近正确的答案。

The first problem is that you never reset root so you will execute the inner loop only once. 第一个问题是你永远不会重置root所以你只会执行一次内循环。 Moving root = 0 to the outer loop should fix it. root = 0移动到外部循环应该修复它。

The second error is that you never break off the loop when you reach the condition you seek. 第二个错误是当你达到你想要的条件时,你永远不会中断循环。 Moving the test to inside the loop will fix this. 将测试移到循环内将解决这个问题。

Lets see how are we doing so far: 让我们看看我们到目前为止的表现如何:

x = int(raw_input('Enter a positive integer: '))
pwr = 0
while pwr < 6:
    root = 0
    pwr += 1
    while root**pwr < x:
        root += 1
        if root**pwr == x:
            print "The root is {} and the power is {}".fornat(
               root, pwr
            )
else:
    print "No such pair of integers exists."

This outputs: 这输出:

Enter a positive integer: 16
The root is 16 and the power is 1
The root is 4 and the power is 2
The root is 2 and the power is 4
No such pair of integers exists.

Since it is a learning exercise, I will let you figure out and fix other problems with your code. 由于这是一个学习练习,我会让你弄清楚并修复你的代码的其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM