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