qq_38436167
2019-10-24 21:12
采纳率: 33.3%
浏览 335

为什么运行不出来,请大佬帮忙指出问题并改正

def welcome():
        print('欢迎来到单位转换器!'.center(40,'-'))
        print('请参照以下样例进行输入:温度转换:45C, 5F, 长度转换:4foot, 1m,  货币转换:1RMB, 1D')


class Temperature:
    """温度转换"""

    def __init__(self, start):
        self.start = start

    def c_to_f(self):
        "摄氏度转华氏度"
        t = self.start.strip('C')
        t1 = ( 9 / 5 ) * t + 32
        print(f'华氏温度为{t1}F')

    def f_to_c(self):
        "华氏度转摄氏度"
        t = self.start.strip('F')
        t2 = ( 5 / 9 ) * t - 32
        print(f'摄氏温度为{t2}C')


class Length:
    """长度转换"""

    def __init__(self, start):
        self.start = start

    def m_to_ft(self):
        "米转英尺"
        l = self.start.strip('m')
        l1 = l * 3.2808399
        print(f'长度为{l1}feet')

    def ft_to_m(self):
        "英尺转米"
        l = self.start.strip('m')
        l2 = l * 0.3048
        print(f'长度为{l2}meters')


class Currency:
    """货币转换"""

    def __init__(self, start):
        self.start = start

    def r_to_d(self):
        "人民币转美金"
        c = self.start.strip('RMB')
        c1 = c * 0.1404
        print(f'共{c1}dollors')

    def d_to_r(self):
        "美金转人民币"
        c = self.start.strip('D')
        c2 = c * 7.1218
        print(f'共{c2}rmb')


class Transfer:
    """单位转换的输入输出"""

    def __init__(self, start):
        self.start = start

    def transfer(self):
        """各种转换"""
        try:
            if self.start.strip().endswith('C') and self.start[:-1].isdigit():
                return Temperature(self.start).c_to_f
            elif self.start.strip().endswith('F') and self.start[:-1].isdigit():
                return Temperature(self.start).f_to_c
            elif self.start.strip().endswith('M') and self.start[:-1].isdigit():
                return Length(self.start).m_to_ft
            elif self.start.strip().endswith('foot') and self.start[:-1].isdigit():
                return Length(self.start).ft_to_m
            elif self.start.strip().endswith('RMB') and self.start[:-1].isdigit():
                return Currency(self.start).r_to_d
            elif self.start.strip().endswith('D') and self.start[:-1].isdigit():
                return Currency(self.start).d_to_r
            elif self.start.strip() == 'q':
                return 'q'
        except Exception as e:
            print('输入格式错误,请重新输入', e)


def main():
    welcome()

    state = input('请按照要求输入:')
    m = Transfer(state)
    while True:
        if m.transfer() == 'q':
            print('退出单位转换器')
            break
        else:
            m.transfer()


if __name__ == "__main__":
    pass

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • threenewbee 2019-10-24 22:26
    已采纳

    if name == "__main__":
    pass

    这里,你没有调用main(),所以程序不会运行

    while True:
        if m.transfer() == 'q':
            print('退出单位转换器')
            break
        else:
            m.transfer()
    

    这里,transfer函数内没有任何改变start变量的逻辑
    所以会陷入死循环

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题