首页 > 解决方案 > Python 新手并制作了一个简单的高度转换器。需要帮忙

问题描述

做一个简单的练习来制作高度转换器,它以英尺为单位接受用户输入并将输入转换为英寸。以下是我到目前为止的内容,但我面临两个问题:

  1. 如果我输入 1 作为我的输入,我的结果是“11111111111”(基本上是 12 次而不是 12 英寸。
  2. 如果我输入一个字符串作为我的输入,我会得到所需的输出,但我的代码会一直持续运行。

我怀疑我的 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}')

标签: pythoninputwhile-loop

解决方案


推荐阅读