Today I have nothing to look at Python basics, when learning the Int () method for data type conversions. Found such an interesting thing, the IDE or the interpreter is a small bug. (The specific reason is unknown, left to find the problem later)
Let's talk about the environment.
- Operating system: WINDOWS10
- Python version: 3.6.5
- IDE environment: Pycharm edu 2018.1 x64
- Interpreter: CPython
then the int () method acts
Function |
Description |
Int (X[,base]) |
J converts x to an integer |
Gossip less about the code:
Scenario 1:
# -*-coding:utf-8-*->>>age=input ()>>>print(int (age))15
Output normal
Scenario 2:
# -*-coding:utf-8-*->>>print(int (15.6)) # when using int () conversion, Forces the number after the decimal point to be omitted
Output normal
Scenario 3:
# -*-coding:utf-8-*-age = input (" Please enter ages:")if int >=18 : Print (" congratulations on your manhood ") Else : Print (" you still need to grow ")
If the user enters a normal integer string without a decimal point, there is no problem with the conversion output.
When the user enters a floating-point string, a strange error occurs when the data type conversion begins:
#-*-coding:utf-8-*-Age = Input ("Please enter your age:")ifInt (age) >=18: #这里的age是字符串类型, but is treated as a number in a conditional expression Print("Congratulations on your manhood.")Else: Print("you still need to grow")#after executing the above parameters, enter the value 15.6, the following error (not only the input string with a decimal point, as long as the non-integer characters, will be an error)Traceback (most recent): File ' c:/pythonstudy/startpython/day1.py ', line 6, ' in <module> if int ' (age ) >=18 :valueerror:invalid literal for Int () with base: ' 15.6 ' #值错误: Int () invalid text with base 10: ' 15.6 '
However, there is no problem in the case of 1 when entering a decimal number for conversion. What is the problem?
Find a solution through Baidu
##-*-Coding:utf-8-*-Import re #引入正则模块Age = Input ("Please enter your age:") TotalCount= Re.sub ("\d","", age) #使用正则的sub方法过滤掉输入的字符串中age带的非数字字符和空的值. ifInt (totalcount) >=18: Print("Congratulations on your manhood.")Else: Print("you still need to grow")
When you enter a string with a decimal point, no more error errors will occur. (But the random input of non-digital symbols will be an error). But printing totalconunt, you will find that this method not only removes the string age with non-numeric characters and null values, even the decimal point is removed.
But with the following code:
# -*-coding:utf-8-*-age = Int (float (input (" Please enter ages:")))if int ( Age) >=18: print(" congratulations on your manhood ")Else : print(" you still need to grow ")
You will find that you can enter a decimal point again. It's not going to be an error.
Initially, the Int () function converts the string of a pure integer number, without a decimal point, to an error. Int (the method should be that the decimal point does not belong to a pure integer category). Float () can convert a pure integer numeric string or a pure floating-point string with a decimal point.
Python's Way of learning Int () Strange error