Kaleem
Kaleem

Reputation: 139

Python code neither running proper nor showing errors in VS Code

Screenshot of Code & outputI am using Vs code(Charm was not working smoother on my PC) for python development it stuck after debugging without showing errors or output.

I searched in stack overflow for a matching solution

weight = int(input("Enter weight: "))
unit   = input("(K)kilograms or (P)pounds? ")

if unit.upper == "P":
    (weight*=1.6)
    print("Weight in kilograms: " + weight)
else if Unit=="K":
    (weight*=1.6)
    print("Weight in pounds: " + weight)
else:
    print("ERROR INPUT IS WRONG!")

I expect it to take input and give converted output

Upvotes: 1

Views: 1212

Answers (1)

Patrick Artner
Patrick Artner

Reputation: 51683

Your script:

  • misses a ()
  • uses an unknown name Unit
  • tries to add string and numbers : print("Weight in pounds: " + weight)
  • does the pound calculation wrong
  • uses () where not applicable
  • uses else if ... :

weight = int(input("Enter weight: "))
unit   = input("(K)kilograms or (P)pounds? ")

if unit.upper == "P":                         # unit.upper()
    (weight*=1.6)                             # ( ) are wrong, needs /
    print("Weight in kilograms: " + weight)   # str + float?
else if Unit=="K":                            # unit  ... or unit.upper() as well
    (weight*=1.6)                             # ( ) are wrong
    print("Weight in pounds: " + weight)      # str + float
else:
    print("ERROR INPUT IS WRONG!")

You can simply use .upper() directly with your input:

# remove whitespaces, take 1st char only, make upper 
unit   = input("(K)kilograms or (P)pounds? ").strip()[0].upper() 

Even better probably:

weight = int(input("Enter weight: "))
while True:
    # look until valid
    unit = input("(K)kilograms or (P)pounds? ").strip()[0].upper()
    if unit in "KP":
        break
    else: 
        print("ERROR INPUT IS WRONG! K or P")

if unit == "P":                          
    weight /= 1.6                           # fix here need divide
    print("Weight in kilograms: ", weight)  # fix here - you can not add str + int
else:  
    weight *= 1.6                        
    print("Weight in pounds: ", weight) 

You should look into str.format:

    print("Weight in pounds: {:.03f}".format(weight))  # 137.500

see f.e. Using Python's Format Specification Mini-Language to align floats

Upvotes: 1

Related Questions