5 Replies - 3498 Views - Last Post: 04 February 2016 - 04:05 AM Rate Topic: -----

#1 tylernuttall   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-February 16

global name 'true' is not defined

Posted 03 February 2016 - 03:47 PM

I am trying to create a script in pythonwin that will check for duplicates in this list but cannot seem to get my code to work. Please help.


a = (2, 8, 64, 16, 32, 4, 16, 8)
def FindDuplicates(a):
    b=set(a)
    if len(B)/>/> == len(a):
        print('There are no duplicates in this list')
        return false
    else:
        print('There are duplicates in this list')
        return true
        









Is This A Good Question/Topic? 0
  • +

Replies To: global name 'true' is not defined

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,317
  • Joined: 12-December 12

Re: global name 'true' is not defined

Posted 03 February 2016 - 04:21 PM

In what way isn't it working?
Where do you call your function FindDuplicates? You've defined it, but you need to call it.
Was This Post Helpful? 1
  • +
  • -

#3 tylernuttall   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-February 16

Re: global name 'true' is not defined

Posted 03 February 2016 - 04:28 PM

View Postandrewsw, on 03 February 2016 - 04:21 PM, said:

In what way isn't it working?
Where do you call your function FindDuplicates? You've defined it, but you need to call it.

I am new to Python so I am still trying to understand some fundamentals. I realized i needed to call the function and attempted to:
a = (2, 8, 64, 16, 32, 4, 16, 8)
def FindDuplicates(a):
    b=set(a)
    if len(B)/> == len(a):
        print('There are no duplicates in this list')
        return false
    else:
        print('There are duplicates in this list')
        return true
FindDuplicates(a)


I got this error with that code:
Traceback (most recent call last):
File "C:\Python27\ArcGIS10.3\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
exec codeObject in __main__.__dict__
File "E:\python\challenge3.py", line 10, in <module>
FindDuplicates(a)
File "E:\python\challenge3.py", line 9, in FindDuplicates
return true
NameError: global name 'true' is not defined
>>>
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6829
  • View blog
  • Posts: 28,317
  • Joined: 12-December 12

Re: global name 'true' is not defined

Posted 03 February 2016 - 04:44 PM

Quote

NameError: global name 'true' is not defined

The constants are True and False not true and false.

Note that I have changed your topic title to something more meaningful than "trouble getting python script to work".
Was This Post Helpful? 2
  • +
  • -

#5 tylernuttall   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-February 16

Re: global name 'true' is not defined

Posted 03 February 2016 - 04:45 PM

View Postandrewsw, on 03 February 2016 - 04:42 PM, said:

Quote

NameError: global name 'true' is not defined

The constants are True and False not true and false.


Andrewsw you are my hero! I would have never figured that was the problem. Thank you!!
Was This Post Helpful? 0
  • +
  • -

#6 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7505
  • View blog
  • Posts: 15,553
  • Joined: 16-October 07

Re: global name 'true' is not defined

Posted 04 February 2016 - 04:05 AM

Note, if you find yourself doing essentially [il]if (condition): return condition[il] you might consider reworking it.

e.g.
a = (2, 8, 64, 16, 32, 4, 16, 8)
def FindDuplicates(a):
    result = len(set(a)) == len(a)
    if result:
        print('There are no duplicates in this list')
    else:
        print('There are duplicates in this list')
    return not result


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1