Keep getting Nameerror: name error is not defined

Hi guys,

I’m 40 minutes in to learning python with Mosh and I keep on getting this error when I run my script for the weight converter excercise:

weight = int(input('Weight: ‘))
unit = input(’(K)g or (L)bs: ') # type: kgs
if unit.upper() == ‘K’:
converted = weight / 0.45
print("Weight in Lbs: " + str(converted))
else:
converted = weight * 0.45
print('Weight in Kgs: ’ + str(converted))

Run:
Weight: 80
(K)g or (L)bs: k
Traceback (most recent call last):
File “/Users/Oli/PycharmProjects/test1/main.py”, line 3, in
unit = input(’(K)g or (L)bs: ') # type: kgs
File “”, line 1, in
NameError: name ‘k’ is not defined

Am I missing something? I’d be so grateful if anyone could help me out!

1 Like

Hey @HumungousChungas, Welcome to the forum.

I believe the issue is with inconsistent quotes.

here is my edited code:

weight = int(input('Weight: '))
unit = input('(K)g or (L)bs: ')  # type: kgs
if unit.upper() == 'K':
    converted = weight / 0.45
    print("Weight in Lbs: " + str(converted))
else:
    converted = weight * 0.45
    print('Weight in Kgs: ' + str(converted))

Hope this helps.

2 Likes

Thanks man, I found the solution on reddit tho - my pycharm was working on python 2.7 which, unbeknowns to me, was on my mac (apparently it’s included…?)

I changed the pycharm interpretter to the new python 3 and it works a charm as is!

Thanks for you help tho!

1 Like

You only make that mistake once :wink:. I’m glad you were able to find the solution.

Happy coding!

1 Like