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

推荐订阅源

爱范儿
爱范儿
WordPress大学
WordPress大学
博客园 - 【当耐特】
The Cloudflare Blog
B
Blog
Last Week in AI
Last Week in AI
小众软件
小众软件
量子位
S
SegmentFault 最新的问题
V
Visual Studio Blog
博客园 - 叶小钗
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
Hugging Face - Blog
Hugging Face - Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
宝玉的分享
宝玉的分享
A
About on SuperTechFans
雷峰网
雷峰网
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Martin Fowler
Martin Fowler

博客园 - xiaobin80

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