python - Python 新手并制作了一个简单的高度转换器。需要帮忙
问题描述
做一个简单的练习来制作高度转换器,它以英尺为单位接受用户输入并将输入转换为英寸。以下是我到目前为止的内容,但我面临两个问题:
- 如果我输入 1 作为我的输入,我的结果是“11111111111”(基本上是 12 次而不是 12 英寸。
- 如果我输入一个字符串作为我的输入,我会得到所需的输出,但我的代码会一直持续运行。
我怀疑我的 while 循环中缺少一个“中断”,但不确定。有任何想法吗?
def height_converter():
#While loop to make sure user inputs the correct units (integers, not strings)
#Input function for accepting height in feet
#Output: Return height in inches
height_unit = 'string'
while height_unit.isdigit() == False:
height_unit = input('Please enter your height in feet: ')
if height_unit.isdigit() == False:
print('Please enter your height in numbers: ')
if height_unit.isdigit() == True:
new_height = 12*(height_unit)
print(f'Your height in inches is {new_height}')
解决方案
推荐阅读
- c# - 读取用户输入的问题
- c++ - 为什么不在 operator= 中返回 const-reference 而不是引用?
- node.js - 使用 socket.io nodejs 将网络摄像头流和聊天发送到特定房间
- python - 如何在 Windows 10 stackoverflow 中离线安装 mxnet-1.4.1(CPU-Version)?
- python - pathlib path.glob 使用 for 循环问题迭代结果
- regex - 如何对未知数量的字符进行模式匹配
- postgresql - psql 在运行一个 sql 文件时向我显示 : 并询问用户输入
- reactjs - 自动完成 - 将左图标添加到自动完成组件中的文本字段
- javascript - Cheerio 获取图像源
- c - 合并排序 - 得到不正确的结果和损坏的数据(复制元素)