Determine whether Python input is a number or character (including regular expressions)

When typing strings, we can judge for ourselves!

One: We treat the input numbers as strings in the program

import re
print("我现在要写一个文件数字猜游戏数字游戏:")


temp=input("请你输入一个数字,猜对了有奖,猜错了,没有关系:")
guess=str(temp)


while guess != '8': temp=input("还没有猜对,继续猜猜看,不要放弃:") guess=str(temp) if guess == '8': print("你猜对了!") else: if guess > '8': print("数字猜大了!") else: print("数字猜小了")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

Two: use isdigit() method

str=input("请输入数字:")
if str.isdigit():
    print("对了,你输入的是数字") else: print("你输入的不是数字")
  • 1
  • 2
  • 3
  • 4
  • 5

Three: Two ways to calculate the length

The first: use the str function to convert the number into a string, and then use the len function to determine the bit length.

a=Int(raw_input("the number you want type in:")  
b=len(str(a))  
print b  
  • 1
  • 2
  • 3

The second: divisor judgment

c=0    
a=int(raw_input("the number you want type in:"))  
while a!=0: a=a/10 c +=1 print c 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Four: After receiving the raw_input method, determine whether the received string is a number

E.g:

str = raw_input(“please input the number:”)

if str.isdigit():

If it is True, it means that all characters entered are numbers, otherwise, not all characters are numbers.

str is a string
str.isalnum() all characters are numbers or letters
str.isalpha() all characters are letters
str.isdigit() all characters are numbers
str.islower() all characters are lowercase
str.isupper( ) all characters are uppercase
str.istitle() all words are first letter uppercase, like title
str.isspace() all characters are whitespace, \t, \n, \r

The above is mainly for integer numbers, but it is not applicable to floating point numbers. So how to judge floating point numbers? I have been struggling with this problem. Why do we have to distinguish between integers and floating point numbers? Yes, isn't it the same for all floating-point numbers? After getting the result, isn't it the same to directly convert to int? Why do you have to decide whether it is an integer or a floating-point number in the early stage? With such an idea, the following is easy to do. , for example:

We can judge by exception. The exception syntax is as follows:

try:
{statements}
exception: {Exception Objects}
{statements}

str = raw_input(“please input the number:”)

try:
f = float(str)
exception ValueError:
print("The input is not a number!")

==========================================================

There is also a pure way to determine whether it is a floating point number, using regular expressions:

Quoting the re regular module

import re

float_number = str(input(“Please input the number:”))

call regular

value = re.compile(r'^[-+]?[0-9]+\.[0-9]+$')

result = value.match(float_number)

if result:
    print "Number is a float."

else: print "Number is not a float."
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  1. About this regular expression, explain:

^[-+]?[0-9]+.[0-9]+$

^ means starting with this character, that is, starting with [-+], [-+] means one of the characters - or +,

? means 0 or 1, which means the symbol is optional.

Similarly [0-9] represents a number from 0 to 9, + represents 1 or more, that is, the integer part.

. represents a decimal point, \ is an escape character because . is a special symbol (matches any single character except \r\n),

So it needs to be escaped.

The same is true for the decimal part, $ means the end of the string.

The regular is not used much, please correct me if there is any mistake.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325137421&siteId=291194637