简体   繁体   English

如何在python中获取无理数作为用户输入?

[英]How to get a irrational number as a user input in python?

How to get a irrational number as a user input in python?如何在python中获取无理数作为用户输入? Like squared root of 2像 2 的平方根

something like :就像是 :

Irrational_Number = float(input("ENTER a Irrational Number : "))
   >>> ENTER a Irrational Number : (USER INPUT)

and then user put a Number like N-th root of K (i mean the number in this format not the exactly this kind of String) " Pi " , " e " or " Phi " in command Line.然后用户在命令行中输入一个类似K 的 N 次根的数字(我的意思是这种格式的数字而不是这种字符串)“ Pi ”、“ e ”或“ Phi ”。

How User Can do this Directly in command line python (3.8)用户如何直接在命令行中执行此操作 python (3.8)

I searched this question everywhere but there is nothing about that...我到处搜索这个问题,但没有关于那个......

First of all, you're casting your input to a float , so to be precise (which is important if we're bandying around terms like Irrational Number ) then we have to highlight the assumption that were talking about approximations to Irrational Numbers here.首先,您将输入转换为float ,所以准确地说(如果我们围绕无理数之类的术语进行讨论,这很重要)然后我们必须强调这里讨论无理数近似值的假设。

That done, there's a few ways you can explore a solution:完成后,您可以通过几种方式探索解决方案:

The first would be to define a namespace in which you map string names to numeric equivalents - a simple dict could do the trick, like:第一个是定义一个命名空间,在其中将字符串名称映射到数字等效项 - 一个简单的 dict 可以解决问题,例如:

numbers_as_words  = { "e",2.718281828459045
                      "pi",3.141592653589793
                      "phi",1.618033988749895
                      "sqr2",1.4142135623730951
                      "feet_to_meters",0.3048
                      "lbs_to_kg",0.453592
                      "miles_to_kilometers",1.60934
                      "gallons_to_litres",3.78541 }

I've padded that out with some non-irrational place-holders too, in case you wanted to embed some simple conversion type logic.我也用一些非理性的占位符填充了它,以防你想嵌入一些简单的转换类型逻辑。

Then, you'd need to take your input as a string, perform a simple replace function on the string to resolve any matching strings, and viola然后,您需要将输入作为字符串,对字符串执行简单的替换功能以解析任何匹配的字符串,然后中提琴在此处输入图片说明 , you've got a mixed numeric/string input method. ,你有一个混合的数字/字符串输入法。

def replace_names_and_return_float(user_input, mapping):
    for k,v in mapping.items():
        user_input = user_input.replace(k,str(v))
    return float(user_input)

Irrational_Number = replace_names_and_return_float(input("ENTER a Irrational Number : "), numbers_as_words )

Then it's just up to you to maintain that numbers_as_words dictionary to contain all the substitutions you want to recognise.然后由您维护该numbers_as_words字典以包含您想要识别的所有替换。 That could get tedious, but it ought to be enough to get you started.这可能会变得乏味,但它应该足以让你开始。

If you find some official list of irrational number approximations, you might be able to download it, and construct a numbers_as_words mapping as an automated process, but I've not Googled that just now.如果您找到一些无理数近似的官方列表,您也许可以下载它,并构建一个numbers_as_words映射作为一个自动化过程,但我现在还没有在谷歌上搜索。

Please find below code snippet:请找到以下代码片段:

import re
g = input("Enter your irrational number : ") 
if g=='pi':
    g=math.pi
elif "root" in g:
    root=float(re.search('[0-9]+', g).group())
    number=float(re.search('(\d+)(?!.*\d)', g).group())
    g=number**(1/float(root))
print(g)

Advantage of using this would be:使用它的好处是:

  1. There is no manual value inserted没有插入手动值
  2. You can build it for every symbol present in math lib您可以为数学库中存在的每个符号构建它
  3. Can be extended for other operations as well也可以扩展到其他操作

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

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