惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
G
Google Developers Blog
Engineering at Meta
Engineering at Meta
月光博客
月光博客
腾讯CDC
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
爱范儿
爱范儿
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Martin Fowler
Martin Fowler
A
About on SuperTechFans
博客园 - 叶小钗

博客园 - xiaobin80

Hello MFC - Add Resource and About Dialog Hello MFC - The Message Map my first mfc application Development Environment Preparation Clang IDE的选择在Windows Visual Studio 2017 and Fx EOL 跟我学python(4)- 正则 跟我学python(3)--- 带shell功能的hello world 跟我学python(2)- 第一个程序 跟我学python --- 搭建开发平台 Debian server 安装 debian 与 ubuntu 之 - sudo Try Value 计算文件CRC32数值 docker ce on Debian 使用hugo在gitee上写blog spring cloud gateway - RequestRateLimiter postman使用 改进的冒泡算法 Create User - mysql COM调用 – VB、PB J-Link clone问题 修复山寨版的J-Link
跟我学python(5)- 匹配数字
xiaobin80 · 2026-02-03 · via 博客园 - xiaobin80

读取输入的字符串,是否是数字;

转换为浮点数并输出。

raw_input

input([prompt])

re.match

re.match(pattern, string, flags=0)

类型转换

  • (1)字符串转浮点
float(x) 
  • (2)数字转字符串
str(x)

代码

#coding=utf-8
'''
Created on 2019年1月3日

@author: xiaobin
'''
import re
'''
#mre22_1.pl
#! /usr/bin/perl -w

# Mastering Regular Expressiona: Chapter 2 Section 2.
# first program

print "Enter a temperature in Celsius:\n";
$celsius = <STDIN>;
chomp($celsius);

if ( $celsius =~ /^[0-9]+$/) {
    $fahrenheit = ($celsius * 9 / 5) + 32;
    print "$celsius C is $fahrenheit F\n";
}
else {
    print "Expecting a number, so I don't understand \"$celsius\".\n";
}
'''
# str1 = raw_input("Enter a temperature in Celsius: ") # python 2
str1 = input("Enter a temperature in Celsius: ")

res = re.match('^[0-9]+$', str1)

if res :
  celsius = float(str1)
  fahrenheit = (celsius * 9 / 5) + 32
  print(str1 + "C is " + str(fahrenheit) + "F")
else :
    print("Expecting a number, so I don't understand " + '\"' + str1 + '\"')

output:

Enter a temperature in Celsius: 38
38C is 100.4F

Reference:

  1. python files io
  2. search vs match
  3. python numbers