Declaring string problem?

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

Declaring string problem?

Postby Person1001 » Thu Jul 04, 2013 10:17 am

Hi. I have been trying to run this code:

name == 'Imran'

if name == 'Imran':
print "You're Imran"
elif name == 'Asif':
print "You're Asif"
else print "You are not familiar"


however it returns with an error that 'name' isnt defined - but havent I defined it as a string in the first line?
Person1001
 
Posts: 7
Joined: Thu Jul 04, 2013 10:14 am

Re: Declaring string problem?

Postby stranac » Thu Jul 04, 2013 10:56 am

Please learn how to use code tags.
They're explained in the sticky post you were supposed to read before posting.
It also explains that you should post your entire error traceback.

Your problem is this piece of code:
Code: Select all
name == 'whatever'

'==' is used for equality testing.
The assignment operator is '='.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1097
Joined: Thu Feb 07, 2013 3:42 pm

Re: Declaring string problem?

Postby Yoriz » Thu Jul 04, 2013 12:30 pm

If the code you have posted is indented correctly
Code: Select all
name == 'Imran'

if name == 'Imran':
    print "You're Imran"
elif name == 'Asif':
    print "You're Asif"
else:
    print "You are not familiar"

You will get
Code: Select all
Traceback (most recent call last):
  File "xxxxxxxxxxxxxxxxxxxxxxxxx", line 2, in <module>
    name == 'Imran'
NameError: name 'name' is not defined


You havent asigned name, you have two '=' which comapares the meerkat.
You want a single '=' to asign something
Code: Select all
name = 'Imran'

if name == 'Imran':
    print "You're Imran"
elif name == 'Asif':
    print "You're Asif"
else:
    print "You are not familiar"
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: 781
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Declaring string problem?

Postby Person1001 » Fri Jul 05, 2013 6:56 pm

Thanks a lot. And sorry I should have used code tags.
Person1001
 
Posts: 7
Joined: Thu Jul 04, 2013 10:14 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], snippsat, Yoriz and 3 guests