关于我制作的小型转换器的IF语句问题 [英] IF statement problem about a small converter I made

查看:101
本文介绍了关于我制作的小型转换器的IF语句问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个程序,将您的体重从公斤转换为磅,反之亦然.它会提示您输入重量,询问重量是千克还是磅,然后得出结果.

I am making a program that converts your weight from kg to lbs and vice versa. It prompts you to input a weight, asks if it's in kg or lbs, then gives you the result.

代码如下:

weight = int(input("What is your weight? "))
unit = input ("(L)bs or (K)g? ")
unit = unit.upper
if unit == "L":
    converted = (weight * 0.45)
    print(converted)

else:
    converted = (weight // 0.45)
    print(converted)

如果我以公斤为单位表示转换器,转换器可以正常工作,但是当我以磅为单位表示磅单位时,它会以公斤为单位并以磅为单位给出答案.谁能告诉我问题是什么?

The converter is working fine if I put my kg and say it's kg, but when I put my weight in lbs and say it is in lbs, it assumes the value is in kg and gives me the answer in lbs. Can anyone tell me what the issue is?

推荐答案

您应在unit.upper的末尾添加()".

没有'()'的情况下,您不会调用upper方法,而只是引用它.

You should add '()' to the end of unit.upper.

Without '()', you are not calling the upper method, but only referencing it.

unit.upper将返回('str对象的内置方法upper在0x00630240' ),

unit.upper will return ('built-in method upper of str object at 0x00630240'),

,因此,当您设置unit = unit.upper时,

and so, when you set unit = unit.upper,

实际上是在设置unit ='str对象位于0x00630240的内置方法上端',

you are actually setting unit = 'built-in method upper of str object at 0x00630240',

激活了else语句.

weight = int(input("What is your weight? "))
unit = input ("(L)bs or (K)g? ")
unit = unit.upper()
if unit == "L":
    converted = (weight * 0.45)
    print(converted)

else:
    converted = (weight // 0.45)
    print(converted)

这篇关于关于我制作的小型转换器的IF语句问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆