python - python3 类型转换是否输入了 input() 值?

标签 python python-3.x input

我正在编写一个基于雨量器半径的雨量器降水计算器。当我运行我的脚本时,出现以下错误消息:

Type de raingauge radius [cm]: 5.0
Traceback (most recent call last):
  File "pluviometro.py", line 27, in <module>
    area_bocal = (pi * (raio_bocal * raio_bocal)) # cm.cm
TypeError: can't multiply sequence by non-int of type 'str'

我正在使用 raio_bocal = input("Type de raingauge radius [cm]:") 用于数据输入。使用 Python2 时,类型转换是自动的。

如何使用 python3 输入一个 float 值?

最佳答案

docs 中所述

The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that

所以你需要明确地将其类型转换为float

raio_bocal = float(input("Type de raingauge radius [cm]:"))

关于python - python3 类型转换是否输入了 input() 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29318565/

相关文章:

python - 在 AWS Glue 中使用纯 python 外部库时 HDFS 中的权限错误

python - 如何从嵌套列表中提取所有子列表?

Python:绘制非重叠圆圈 - 递归失败, 'while' 有效

python-3.x - 带有包含 % 符号的 LIKE 子句的 psycopg2 insert 语句

python - 您可以从 Windows 符号链接(symbolic link)导入 Python 模块吗?

c++ - Shell 输入作为变量

python - 使用逻辑表达式和 if 语句评估 pandas 系列值

java - 如何在线程 “main”中修复异常java.util.InputMismatchException

ios - Mobile Safari (10.3.1) DateTime-Local "Enter a Valid Value"错误

python-3.x - 我的条形图未显示所有数据值的条形图