python-3.x 没有收到我应该[复制]的输出

n9vozmp4  于 2023-02-06  发布在  Python
关注(0)|答案(2)|浏览(116)
    • 此问题在此处已有答案**:

Why does "a == x or y or z" always evaluate to True? How can I compare "a" to all of those?(8个答案)
1年前关闭。
所以我试着做一个体重转换器应用程序。它首先询问用户他们的体重,然后询问是磅还是千克,然后将其转换为其他单位。例如,如果有人以千克输入他们的体重,它将以磅转换。但问题是,每当我以磅输入时,它应该将其转换为千克,但它没有,并"转换"为磅,尽管它是以磅为单位的。

weight = int(input("Enter your weight: "))
user_choice = input ("(L)bs or (K)g: ")

if user_choice == "K" or "k":
    converted = int(weight) * 2.2046
    print (f"Your weight in pounds is {converted} lbs")
elif user_choice == "L" or "l":
    converted = int(weight) / 2.2046
    print (f"Your weight in kilograms is {converted} kg")
else:
    print ("Please enter a valid option.")

我是一个初学者在python所以任何帮助将不胜感激。

ruarlubt

ruarlubt1#

使用用户选择==“k”或用户选择==“K”

weight = int(input("Enter your weight: "))
    user_choice = input ("(L)bs or (K)g: ")
    
    if (user_choice == "K" or user_choice=="k"):
        converted = int(weight) * 2.2046
        print (f"Your weight in pounds is {converted} lbs")
    elif(user_choice == "L" or user_choice=="l"):
        converted = (int(weight) / 2.2046)
        print (f"Your weight in kilograms is {converted} kg")
    else:
        print ("Please enter a valid option.")
brc7rcf0

brc7rcf02#

您可以通过稍微修改一下if-elif条件来使其工作

weight = int(input("Enter your weight: "))
user_choice = input ("(L)bs or (K)g: ")

if user_choice in ('k', 'K'):
    converted = int(weight) * 2.2046
    print (f"Your weight in pounds is {converted} lbs")
elif user_choice in ('l', 'L'):
    converted = int(weight) / 2.2046
    print (f"Your weight in kilograms is {converted} kg")
else:
    print ("Please enter a valid option.")

相关问题