python - 在python中将英寸的输入转换为立方英寸
问题描述
这是我得到的,但转换不起作用
a = input('What is your name? ')
b = input('What is the color of the ball? ')
c= input('What is the radius of the ball in inches? ').split()[:2]
converted_data = float=(c) * {'in': 2.54, 'cm': 0.39}[unit]
print('Hello ' + a + ', the ball is ' + b + ' and has a volume of ' + converted_data + ' cubic inches.')
解决方案
我喜欢您制作字典以保存适当的单位转换值的做法。在您的代码中,您需要为变量分配一个值unit
。
如果在cm
? _
最后,要计算volume
given radius
,首先将(如您所想)转换为以英寸为单位的半径,如果需要,然后应用适当的公式。
c, unit = input("What is the radius of the ball? (e.g. '4 in' or '3.14cm') ").split()[:2]
radius = float(c) * {'in': 1, 'cm': 0.3937}[unit]
volume = 4/3 * math.pi * radius**2
print('Hello ' + a + ', the ball is ' + b + ' and has a volume of ' + volume + ' in³.')
推荐阅读
- python - 创建一个包含 1 到 100 的所有倍数且小于 100 的 python 循环/列表理解
- python - 在python中将元组列表保存到csv文件
- node.js - 如何在 MERN 中插入图像路径?
- sql - SQL 中满足条件时的值
- swift - SwiftUI 多定时器管理
- windows - 在 Windows 上使用 Docker Compose 的 Kong:无法加载上游的初始列表:无法从节点缓存中获取:无法获取回调锁
- powershell - 为什么将powershell 7设置为默认值时,ansible win_shell模块使用powershell 5?
- c - 简单的 C 程序使用
方法未运行 - r - 如何从列的特定部分找到最大值(行)?
- python - 将包含嵌套列表列表的字典转换为 df