我想在Python中找到cuberoot ... [英] I want to find cuberoot...in Python

查看:191
本文介绍了我想在Python中找到cuberoot ...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在phython我知道找到一个完美立方体的立方根,但我怎么能找到一个数字的立方根,如9,7,17​​ ...这不是一个完美的立方体我想要大致的回答如下:

例如:



输入:num = 9

输出:2.08



我尝试过:



in phython i know find a cube root of perfect cube but how i can find a cube root of number like 9,7,17...which are not a perfect cube i want approx answer like:
example:

input:num=9
output:2.08

What I have tried:

cube=int(input(' enter a vlue to find it cube root,(vlue must be >1):'))
epsilon=0.01
num_guess=0
low=0
high=cube
guess=(high+low)/2.0
while abs(guess**3-cube)>=epsilon:
	if guess**3<cube:
			low=guess
	else:	#guess**3>=guess
		high=guess
		guess=(high+low)/2.0
		num_guess+1

print('number of guess=',num_guess)
print(guess)

推荐答案

同样的问题,同样的答案。

是的,我可以提供解决方案,但这将是魔术,你不会学到任何东西。相反,我给你一把钥匙,让你自己找出错误,并相信我,它更有价值,并将大大改善你的学习曲线。

试一试,你应该几分钟内找到问题。



[上一个回答]

使用Python,缩进是你程序的结构,需要额外的关注对它来说。

使用调试器并逐行执行,你会看到有什么问题。



有一个工具可以让你要查看代码正在执行的操作,其名称为调试程序。它也是一个很好的学习工具,因为它向你展示了现实,你可以看到哪种期望与现实相符。

当你不明白你的代码在做什么或为什么它做它做的时候,答案就是答案是调试器

使用调试器查看代码正在执行的操作。只需设置断点并查看代码执行情况,调试器允许您逐行执行第1行并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

27.3。 pdb - Python调试器 - Python 3.6.1文档 [ ^ ]

使用Python进行调试Python征服宇宙 [ ^ ]

pdb - 交互式调试器 - 本周的Python模块 [ ^ ]

调试器在这里显示你的内容代码正在做,你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它没有发现错误,它只是帮助你。当代码没有达到预期的效果时,你就接近了一个错误。
Same question, same answer.
Yes, I can gives the solution, but it will be magic and you will not learn a single thing. Instead I give you a key that will allow you to find what is wrong by yourself, and believe me, it is much more valuable and will dramatically improve your learning curve.
Give it a try, you should find the problem in matter of minutes.

[Previous answer]
With Python, indentation is the structure of your program, pay extra care to it.
Use the debugger and execute line by line, you will see what is wrong.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
27.3. pdb — The Python Debugger — Python 3.6.1 documentation[^]
Debugging in Python | Python Conquers The Universe[^]
pdb – Interactive Debugger - Python Module of the Week[^]
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


我找到了解决方案......不只是为了立方根,而是为了你能找到的所有root。 ..



i found the solution...not for only cube root but for all root which you can find...

n=int(input('enter a value of which root you want to find(ex.for squreroot type 2,cuberoot type 3):'))
d=int(input('enter a no. to find  root of it:'))
x=1/n
print('your ans.=',d**x)

                                         #now its look like pythonikkkkk.....


这篇关于我想在Python中找到cuberoot ...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆