Usage of Python float(input()), application in web

float(input())

To understand float(input()) in Python, it can be divided into two parts. First, input() is used to obtain the input on the keyboard. The return value of this function is a Python string str type data - but what is input; second, the float() function is used to transfer the passed parameters - -Here is the return value of input(), a string - converted to a float type. The float() function converts the return value of input() to preserve the corresponding precision compared to using int().

Similar applications of float(input()) in web

The use of float(input()) in Python programs can generally be used to obtain the user's keyboard input and perform related operations. In Python web projects, such as using Django to develop web, when the front-end passes parameters to the back-end through URL, if it needs to be used for mathematical operations, you can generally use float(input()) to perform the parameter passed by the URL first. Conversion, if you operate directly without conversion, Python may throw a TypeError, or the strings may be spliced ​​together directly through the "+" operation. Of course, Django does not obtain the parameters of the url through float(input()). This is just an analogy.

float(input()) example code

>>> inputNumber = float(input("请输出一个数字:"))
请输出一个数字:5
>>> inputNumber
5.0
>>> type(inputNumber)
<class 'float'>
>>> inputString = input("请输出任意一个数字:")
请输出任意一个数字:6
>>> inputString
'6'
>>> type(inputString)
<class 'str'>

Original text:Usage of Python float(input()), application in web

Disclaimer: Content is for reference only!

Guess you like

Origin blog.csdn.net/weixin_47378963/article/details/134747496