gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:41:48 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com