简体   繁体   中英

This python code isn't working for me and I do not know why. Tell me if there any promblem with the code

I don't know why this python code isn't working, please tell me why.

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

if Weightscale.lower == "L":
                  ans = (weight * 0.45)
                  print(f"You are {ans} Kg")

else:
    ans = (weight / 0.45)
    print(f"You are {ans} pound")

You should declare weight as a float to have decimal values.

You must use () to call lower . Then your lower condition must compare to a lowercase letter "l" , not "L" :

weightscale = input("(L)bs or (K)g: ")
weight = float(input(f"Enter weight in {weightscale}: "))

# Lower needs (), then make the "L" lower too -> "l"
if weightscale.lower() == "l":
    ans = (weight * 0.45)
    print(f"You are {ans} Kg")

else:
    ans = (weight / 0.45)
    print(f"You are {ans} pound")

I notice three mistakes in your code:

  • weight is not declared

  • Weightscale.lower should be a function call, ie Weightscale.lower()

  • Weightscale.lower() will never be equal to "L" , so your if block will never be treated. You should do one of the following:

    • Weightscale.lower() == "l"
    • Weightscale.upper() == "L"

使用较低并比较“L”,尝试:

if Weightscale.lower() == "l":

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM