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
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I am used to the following in Java:
try {
// something bad happens here
} catch (SomeException se) {
se.printStackTrace(); // let's me print the stack trace
System.out.println(se); // let's me print and inspect the exception
throw se; // I can re-throw it or wrap it and throw it
}
I am having a really hard time linking this to Pythons
try:
pass # do something
except ExceptionClass1:
print("OK I know I caught an ExceptionClass1 type exception")
# but how do I get to the stack trace
# how do I get to the object of the exception
You will need the traceback library:
try:
# Do something
except ExceptionClass1 as error:
traceback.print_exc() # Prints the exception
# Raise alone will re-raise the same exception
raise
# Or, raise a new exception
raise RuntimeError("bad things") from error
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
I've tried to using this code:
def tw_list(text):
tw_list['text'] = tw_list['text'].str.lower()
print('Case Folding Result : \n')
print(tw_list['text'].head(5))
print('\n\n\n')
But when I run it, I get the error:
Case Folding Result :
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-27-c3ee7c744c13> in <module>
6
7 print('Case Folding Result : \n')
----> 8 print(tw_list['texts'].head(5))
9 print('\n\n\n')
TypeError: 'function' object is not subscriptable
i dont know why, can someone help me?
tw_list is the name of your function.
You are trying to access the function as a dict.So it throws error
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
So, here I am, playing around with the ipfsapi in python and yesterday, to start somewhere, I ran some code from the following blogpost that connects to the local node: https://medium.com/python-pandemonium/getting-started-with-python-and-ipfs-94d14fdffd10
Last night it worked perfectly, but today when I began a new project where I would actually use this method and rewrote the code, I got an invalid syntax error in the except statement. Right now the code looks like this
if __name__ == '__main__':
try:
api = ipfshttpclient.connect('127.0.0.1', 5001)
print(api)
except: ipfshttpclient.exceptions.ConnectionError as ce: #the invalid syntax error is marked at as
print(str(ce))
Traceback:
File "/home/", line 17
except: ipfshttpclient.exceptions.ConnectionError as ce:
^
SyntaxError: invalid syntax
The odd thing is that I'm getting an invalid syntax error on the as. I've changed ipfsapi due to deprecation warning to ipfshttpclient, but it doesn't work with either now, same erreor. How is that even possible? Am I just not seeing something I should? Is my brain smoothing out? Sorry if it's a dumb issue and thanks in advance!
Python 3.7.4 64-bit | Qt 5.9.6 | PyQt5 5.9.2 | Linux 5.5.10-arch1-1
You should remove the colon : after the except.
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
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 8 years ago.
Improve this question
I'm a beginner computer science student and using python in computer science 1. My assignment is to write a program that creates a spirograph. I think that the code is all right to do that, but when i run it, an error message pops up that says syntax error and it highlights down(), which is a common turtle command. I have no idea why. It said syntax error for main(), but then i restarted python and now it says there's an error in down(). Here's the code:
from turtle import *
from math import *
def xValue(R,r,p,t):
x=(R-r)*cos(t)-(r+p)*cos((R-r)/r*t)
def yValue(R,r,p,t):
y=(R-r)*sin(t)-(r+p)*sin((R-r)/r*t)
def initialPosistion():
t=2*pi
up()
goto(xValue(R,r,p,t),yValue(R,r,p,t)
down()
def iterating(R,r,p):
t = 2*pi
while t < 0:
t = t-0.01
goto(xValue(R,r,p,t),yValue(R,r,p,t)
up()
def main():
R = 100
r = 4
p = int(input("Enter p(10-100): "))
if p < 10 or p > 100:
input("Incorrect value for p!")
iterating(R,r,p)
input("Hit enter to close...")
main()
Missed a closing ) at the end of this line:
goto(xValue(R,r,p,t),yValue(R,r,p,t))