So this is the code im providing and the description is below :)
r = s.get(Url)
names = soup(r.text, 'html.parser')
findName = names.find('div', {'class':'nameslist'})
if (r.status_code == 200):
for name in names.find_all('option'):
global nameID
if myName == foundName:
nameID = size.get('value')
print('Profile-1 Found name')
break
else:
print('Did not find name')
if (findName != None):
notepresent = True
print('There is a value')
else:
global addtonotes
notepresent = False
addtonotes = {'id': nameID}
add2notes(addtonotes)
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Profile-1 Found name
addtonotes = {'id': nameID}
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Did not find name
Traceback (most recent call last):
self._target(*self._args, **self._kwargs)
File "C:\Users\Test.py", line 153
addtonotes = {'id': nameID}
NameError: name 'nameID' is not defined
The simplest approach would be to set nameID
to an empty value before the loop:
nameID = None
for name in names.find_all('option'):
if myName == foundName:
nameID = size.get('value')
print('Profile-1 Found name')
break
if nameID is None:
print('Did not find name')
However, the Python for
loop actually has dedicated syntax for this case; you can add an else:
block to a for
loop. It is only executed when the for
loop completes, i.e. when there was no break
to end the loop early:
for name in names.find_all('option'):
if myName == foundName:
nameID = size.get('value')
print('Profile-1 Found name')
break
else:
print('Did not find name')