NameError: name 'c' is not defined ?

This is the place for queries that don't fit in any of the other categories.

NameError: name 'c' is not defined ?

Postby mrbillones » Sat Jun 07, 2014 7:13 pm

Hello fellow python dev!

good day! i have a question as well as problem, may i know the possible error of this script. the script that ive used is from the tutorial. I watched the video and there is no error happened but when i run the script in my computer. i encountered this error. please help me guys. thank you so much in advance!
Code: Select all
def convertTemp(temp, scale):
   if scale == "c":
      return (temp - 32.0) * (5.0/9.0)
   elif scale == "f":
      return temp * 9.0/5.0 + 32

temp = int(input("Enter a temperature: "))
scale = input("Enter the scale to convert to: ")
converted = convertTemp(temp, scale)
print("The converted temp is: " + str(converted))

Output
Code: Select all
Enter a temperature: 21
Enter the scale to convert to: c
Traceback (most recent call last):
  File "C:\Users\XX\test.py", line 8, in <module>
    scale = input("Enter the scale to convert to: ")
  File "<string>", line 1, in <module>
NameError: name 'c' is not defined
Last edited by Yoriz on Sat Jun 07, 2014 7:23 pm, edited 1 time in total.
Reason: First post lock, Added code tags, Changed Title, Moved.
mrbillones
 
Posts: 3
Joined: Sat Jun 07, 2014 7:05 pm

Re: NameError: name 'c' is not defined ?

Postby Yoriz » Sat Jun 07, 2014 7:24 pm

Welcome to the forum, please read the new users read this link in my signature.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 782
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: NameError: name 'c' is not defined ?

Postby metulburr » Sat Jun 07, 2014 7:31 pm

If you are using python2.x use raw_input(). Or run the program with python3.x as the same in Python3.x is input()

In python2.x input() is like doing:
Code: Select all
eval(raw_input())

which is going to cause an error if the variable c does not exist.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1416
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: NameError: name 'c' is not defined ?

Postby mrbillones » Sun Jun 08, 2014 7:51 am

Hello!

thank you for the quick reply. im using the python 3.4. how do i run it using the python 3.4? because im also have 2.7 installed.
mrbillones
 
Posts: 3
Joined: Sat Jun 07, 2014 7:05 pm

Re: NameError: name 'c' is not defined ?

Postby mrbillones » Sun Jun 08, 2014 8:45 am

metulburr wrote:If you are using python2.x use raw_input(). Or run the program with python3.x as the same in Python3.x is input()

In python2.x input() is like doing:
Code: Select all
eval(raw_input())

which is going to cause an error if the variable c does not exist.


hello sir metulburr!

thanks for the help! this is what i did in my code. so i guess im using python2.7 though i set the directory in python 3.4.
Code: Select all
def convertTemp(temp, scale):
   if scale == "c":
      return (temp - 32.0) * (5.0/9.0)
   elif scale == "f":
      return temp * 9.0/5.0 + 32

temp = int(input("Enter a temperature: "))
scale = (raw_input("Enter the scale to convert to: "))
converted = convertTemp(temp, scale)
print("The converted temp is: " + str(converted))

OUTPUT:
Code: Select all
C:\Python34>test.py
Enter a temperature: 212
Enter the scale to convert to: c
The converted temp is: 100.0


Yoriz wrote:Welcome to the forum, please read the new users read this link in my signature.
Last edited by Yoriz on Sun Jun 08, 2014 1:13 pm, edited 2 times in total.
Reason: Added code tags.
mrbillones
 
Posts: 3
Joined: Sat Jun 07, 2014 7:05 pm

Re: NameError: name 'c' is not defined ?

Postby metulburr » Sun Jun 08, 2014 12:51 pm

Code: Select all
temp = int(input("Enter a temperature: "))
scale = (raw_input("Enter the scale to convert to: "))

They ALL need to be raw_input() if python2.x and all need to be input() if python3.x
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1416
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: NameError: name 'c' is not defined ?

Postby Kebap » Sun Jun 08, 2014 2:06 pm

Hello! Please make sure you use a tutorial for python 3 if you want to use python 3, or a tutorial for python 2, if you want to use python 2. The both are much similar, but not 100% the same, and you just found one difference. :geek:
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 390
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests