English 中文(简体)
寻找一用户进入集层的根基/动力 in
原标题:Python exercise to find root/power integer pair for a user entered integer
  • 时间:2024-03-18 02:06:10
  •  标签:
  • python

The exercise as was written:

inger: 起草一项方案,要求用户输入2个ger、根和粉碎机,如1 <pwr <6和根**pwr等于用户输入的ger。 如果不存在这种分类,它就应当为此印制信息。

这是我提出的:

    num_value = int(input("Enter a number value:"))
    result_state = "unknown"
    for root in range (-abs(num_value),abs(num_value)):
        for pwr in range(2,6):
            test_value = root**pwr
            if test_value == num_value:
                print(f {num_value} is equivalent to {root} to the power of {pwr} )
                result_state = "Printed"
    if result_state != "Printed":
        print( I was not able to find a root/power integer pair for this number. )

我所写的守则是行之有效的,但我感到,同样,实现同样结果的方法可能比较有效。 我也肯定地感到,我可能认为这只是增加根本范围绝对功能。 我试图用开放式的电灯从我自己那里学习,但我希望确保我不会根据我的理解发展盲点。 因此,感谢你的帮助!

问题回答

Here is some improvement of your code:

  1. Change result_state from string to boolean. The comparison of boolean is faster than comprison of string.
  2. Change the range of root from range(-abs(num_value),abs(num_value)) to range(-int(abs(num_value) ** 0.5), int(abs(num_value) ** 0.5) + 1). Since the smallest pwr is 2, if root is greater than the square root of abs(num_value), test_value cannot equal to num_value.
  3. Add break to if-statement, if a pair of root and pwr, when root grows, test_value cannot still equals to num_value.

增订的法典如下:

num_value = int(input("Enter a number value: "))
result_state = False

for root in range(-int(abs(num_value) ** 0.5), int(abs(num_value) ** 0.5) + 1):
    for pwr in range(2, 6):
        test_value = root ** pwr
        if test_value == num_value:
            print(f {num_value} is equivalent to {root} to the power of {pwr} )
            result_state = True
            break  # Exit inner loop if pair found

if result_state != True:
    print( I was not able to find a root/power integer pair for this number. )




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签