我是靠谱客的博主 甜甜泥猴桃,这篇文章主要介绍Python 使用eval函数string 转 int,现在分享给大家,希望可以做个参考。

Python 使用eval函数string 转 int

eval(str)

用来计算在字符串中的有效Python表达式,并返回一个对象

#!/usr/bin/python3
import string
while True:
print("Input Number: ")
str1 = input()
print(type(str1))
if str1[-1] in ['F', 'f']:
c = (eval(str1[:-1]) - 32) / 1.8
print(c, "C")
elif str1[-1] in ['C', 'c']:
f = (eval(str1[:-1])) * 1.8 + 32
print(f, "F")
else :
print("Input error")

结果

(test) root@d3b207e335a0:/test# python3 fc.py
Input Number:
10c
<class 'str'>
50.0 F
Input Number:

最后

以上就是甜甜泥猴桃最近收集整理的关于Python 使用eval函数string 转 int的全部内容,更多相关Python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(145)

评论列表共有 0 条评论

立即
投稿
返回
顶部