7 Replies - 319 Views - Last Post: 10 January 2013 - 10:20 AM Rate Topic: -----

#1 Ghostpanda34  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 09-January 13

If and Else not working as planned.

Posted 09 January 2013 - 01:49 PM

Hello all,
I am making the simplest program you can imagine:
/code
name = input("A number for doublifier:")
word = input("say Doublify")
if word == Doublify:
print ("Your doubled number is ", name * 2)
else:
print ("USE THE MAGIC WORD!!!")
/code

The problem is that when I hit run and enter either 'Doublify' or 'no' (else) it come up with:
/code
Traceback (most recent call last):
File "C:\Users\Ben\Desktop\BADouplifier.py", line 2, in <module>
word = input("say Doublify")
File "<string>", line 1, in <module>
NameError: name 'no' is not defined
/code

Hello all,
I am making the simplest program you can imagine:
[code
name = input("A number for doublifier:")
word = input("say Doublify")
if word == Doublify:
print ("Your doubled number is ", name * 2)
else:
print ("USE THE MAGIC WORD!!!")
[code]

The problem is that when I hit run and enter either 'Doublify' or 'no' (else) it come up with:
Traceback (most recent call last):
File "C:\Users\Ben\Desktop\BADouplifier.py", line 2, in <module>
word = input("say Doublify")
File "<string>", line 1, in <module>
NameError: name 'no' is not defined
[code]

Hello all,
I am making the simplest program you can imagine:


name = input("A number for doublifier:")
word = input("say Doublify")
if word == Doublify:
print ("Your doubled number is ", name * 2)
else:
print ("USE THE MAGIC WORD!!!")

The problem is that when I hit run and enter either 'Doublify' or 'no' (else) it come up with:


Traceback (most recent call last):
File "C:\Users\Ben\Desktop\BADouplifier.py", line 2, in <module>
word = input("say Doublify")
File "<string>", line 1, in <module>
NameError: name 'no' is not defined
[/code]

Is This A Good Question/Topic? 0
  • +

Replies To: If and Else not working as planned.

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7301
  • View blog
  • Posts: 12,158
  • Joined: 19-March 11

Re: If and Else not working as planned.

Posted 09 January 2013 - 01:54 PM

That's a little hard to read, but this line strikes me as probably problematic:

if word == Doublify:


unless Doublify is defined as a variable, this will throw a NameError like what you're seeing.

You might mean

if word == "Doublify":


(with the quotes)
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: If and Else not working as planned.

Posted 09 January 2013 - 01:55 PM

You need quotes around "Doublify" and indenting:

name = input("A number for doublifier:")
word = input("say Doublify")
if word == "Doublify":
    print ("Your doubled number is ", name * 2)
else:
    print ("USE THE MAGIC WORD!!!")


BTW the error message doesn't reflect the code you have shown?!

BTWW If you want to double a number then you will have to convert the string name to a number. (input() returns a string.)

This post has been edited by andrewsw: 09 January 2013 - 01:56 PM

Was This Post Helpful? 0
  • +
  • -

#4 Ghostpanda34  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 09-January 13

Re: If and Else not working as planned.

Posted 09 January 2013 - 02:03 PM

View Postandrewsw, on 09 January 2013 - 01:55 PM, said:

You need quotes around "Doublify" and indenting:

name = input("A number for doublifier:")
word = input("say Doublify")
if word == "Doublify":
    print ("Your doubled number is ", name * 2)
else:
    print ("USE THE MAGIC WORD!!!")


BTW the error message doesn't reflect the code you have shown?!

BTWW If you want to double a number then you will have to convert the string name to a number. (input() returns a string.)

Thanks all though it still comes up with:

A number for doublifier:2
say DoublifyDoublify
Traceback (most recent call last):
File "C:\Users\Ben\Desktop\BADouplifier.py", line 2, in <module>
word = input("say Doublify")
File "<string>", line 1, in <module>
NameError: name 'Doublify' is not defined

Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2890
  • View blog
  • Posts: 9,597
  • Joined: 12-December 12

Re: If and Else not working as planned.

Posted 09 January 2013 - 02:13 PM

Try use raw_input() which is, I understand, preferred.

Maybe it's the system you are using to run the code..

Is the code you are showing exactly the same as the code you are testing..?

This post has been edited by andrewsw: 09 January 2013 - 02:13 PM

Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7301
  • View blog
  • Posts: 12,158
  • Joined: 19-March 11

Re: If and Else not working as planned.

Posted 09 January 2013 - 02:42 PM

Could you please just post the whole source code that you're running?
In code tags, if you could be so kind: copy the code from your editory, paste it into the edit window, select it, and press the [CODE] button.
Was This Post Helpful? 0
  • +
  • -

#7 Nallo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 162
  • View blog
  • Posts: 248
  • Joined: 19-July 09

Re: If and Else not working as planned.

Posted 09 January 2013 - 06:27 PM

From the error you got I deduct that you are using Python 2.x. You don't want to use input there but raw_input. raw_input returns a string and then you should typecast it to the approproate type:

x = raw_input("enter a number:")
#typecast to an integer
x = int(x)



q = raw_input("(y)es or (n)o?")
#no typecast needed, we use a string here
if q == "y":
    pass
elif q == "n":
    pass



An explanation of the error you got: input in python 2.x is equivalent to eval(raw_input()). So when you type in "Doublify" python tries to evaluate Doublify as if it where a variable (actually as if it where an expression). But no such name exists, and python complains with an error.
Was This Post Helpful? 1
  • +
  • -

#8 Ghostpanda34  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 09-January 13

Re: If and Else not working as planned.

Posted 10 January 2013 - 10:20 AM

Thanks guys, it turned out I needed raw_imput on line 2.
THANK YOU!!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1