Value entered in a function changed by the default value [closed] - python

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
So I created this function:
def bs_obj(url, lan="html.parser"):
try:
html = urlopen(url)
bsObj = BeautifulSoup(html, lan)
print(lan)
return bsObj
except HTTPError as e:
print(e)
Now, if I call the function with the next code: object = bs_obj(html, "lxml"), the console prints html.parser. Same goes if the code is object = bs_obj(html, lan="lxml"). What's going on?
EDIT: (SOLVED) I'm ashamed. I was calling bs_obj(html) some lines before the codeline I used as example.

I believe you are running the wrong file. For reference.
def bs_obj(lan="html.parser"):
print(lan)
if __name__ == "__main__":
bs_obj()
bs_obj("lxml")
bs_obj(lan='html5.parser')
Correctly outputs
html.parser
lxml
html5.parser

Related

Inheritance not working as tutorial. what is the problem? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm studying 'inheritance' in Pycharm.
I just followed tutorial. And it doesn't work. What's the problem
##Chef.py##
class Chef:
def make_chicken(self):
print("The chef makes a chicken")
def make_salad(self):
print("The chef makes a salad")
def make_special_dish(self):
print("The chef makes bbq ribs")
##another.py##
from Chef import Chef
class another(Chef):
##app.py##
from another import another
a = another()
a.make_salad()
run>>>
error message :
Traceback (most recent call last):
File "C:/Users/NEWS1/PycharmProjects/exc/app.py", line 1, in <module>
from another import another
File "C:\Users\NEWS1\PycharmProjects\exc\another.py", line 9
^
SyntaxError: unexpected EOF while parsing
Process finished with exit code 1
What is the problem....
The issue is in your 'another' class, there's nothing following the colon. You can either add methods to the class or just a 'pass' like so
##another.py##
from Chef import Chef
class another(Chef):
# other content
pass

how to add an attribute to a light using python [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I'm new in python and I'm trying to add an attribute to a maya light shape. The script should works like that: for each light.shape I've selected add a 'mtoa_constant_lightGroup' attribute:
import maya.cmds as pm
lightSelect= pm.ls (sl=True, dag=True, leaf=True)
for elem in lightSelect:
pm.addAttr (elem, ln='mtoa_constant_lightGroup', at=long, dv=0)
pm.setAttr (e=True, keyable=True, elem +'.mtoa_constant_lightGroup')
But when I run the script I've got this error:
Error: line 1: non-keyword arg after keyword arg
Any suggestions please.
In the following line from your code you have a positional argument after a keyword argument, which does not make sense.
pm.setAttr (e=True, keyable=True, elem +'.mtoa_constant_lightGroup')
# ---- here ----------------------^
fix it!
so as Martin said I had to move the keyword arguments to the end
then for the error " # Error: line 1: RuntimeError: file line 6: Type specified for new attribute is unknown." I needed to set at=long as a string, e.g.
`pm.addAttr (elem, ln='mtoa_constant_lightGroup', at="long", dv=0)`
The final scripts is this:
import maya.cmds as pm
lightSelect= pm.ls (sl=True, dag=True, leaf=True)
for elem in lightSelect:
pm.addAttr (elem, ln='mtoa_constant_lightGroup', at="long", dv=0)
pm.setAttr(elem +'.mtoa_constant_lightGroup', e=True, keyable=True)
thanks for all your helps

Why Exception occurs in this simple python program? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I wrote this really simple Python program to learn how Exception works:
def Divide(x,y):
try:
print (int(a)/int(b))
except:
print "Exception Occured!"
But weirdly the exception occurs every time:
>>> Divide(int(1),int(2))
Exception Occured!
>>> Divide(1,2)
Exception Occured!
While it shouldn't occur:
>>> print 1/2
0
>>> print (1/2)
0
>>> print (int(1)/int(2))
0
What's wrong?
a and b are not defined - argument names in the function signature are x and y
def Divide(x,y):
try:
print (int(a)/int(b))
except Exception as e:
print 'Error: ' + str(e)
Try to write same code this way. You'll see why it occurs.
because you should define a & b .
it's error is about global parameters :
global name 'a' is not defined

NameError: global name 'add_to_receives' is not defined | add_to_receives(msg_to) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
def add_to_receivers(receivers): # make sure they are unique
for receiver in receivers:
name, email = receiver
if email not in [u'', None]: # empty receiver pairs are possible
#print email
if email not in msg_receivers and email is not None:
msg_receivers.append(email.lower())
msg_receivers = []
add_to_receives(msg_to)
add_to_receives(msg_cc)
add_to_receives(msg_bcc) # in essence, we don’t care how the message was received
#add_to_receives(msg_delivered_to)
please help me with fixing this error
Traceback (most recent call last):
File "metadata.py", line 309, in worker
add_to_receives(msg_to)
NameError: global name 'add_to_receives' is not defined
full code you can see at http://brage.bibsys.no/xmlui/handle/11250/198551
I think the problem is that the function is called add_to_receiversand you are calling add_to_receives (without the 'r')

NameError: name 'random' is not defined [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Here is my code:
r = random.randint(1,10)
But for some reason it's giving me the error
NameError: name 'random' is not defined
Other info: Mac, Python, 3.4.0 pylauncher
You have to import the module random:
import random
r = random.randint(1,10)
# ...
>>> import random
>>> r = random.randint(1,10)
>>> r
10
you need to import the random library with the import keyword
import random
random.randint(1,10) #no. between 0 and 10

Categories

Resources