I am trying a challenge on hackerrank. The text of the challenge says:
Hint: Try using Email.utils() to complete this challenge. For example, this code:
import email.utils
print email.utils.parseaddr('DOSHI <DOSHI#hackerrank.com>')
print email.utils.formataddr(('DOSHI', 'DOSHI#hackerrank.com'))
produces this output:
('DOSHI', 'DOSHI#hackerrank.com')
DOSHI <DOSHI#hackerrank.com>
However, this is not the output I get when I run those 3 lines. I get the following error:
File "<ipython-input-248-e5b75ae88af9>", line 2
print email.utils.parseaddr('DOSHI <DOSHI#hackerrank.com>')
^
SyntaxError: invalid syntax
Advice? Thank you!
Never mind - they probably created it in Python 2.
The code is missing the parents. The following works:
print(email.utils.parseaddr('DOSHI <DOSHI#hackerrank.com>'))
print(email.utils.formataddr(('DOSHI', 'DOSHI#hackerrank.com')))
I think you are importing email.utils() wrong.
Try this:
import email as e
print e.utils.parseaddr('DOSHI <DOSHI#hackerrank.com>')
print e.utils.formataddr(('DOSHI', 'DOSHI#hackerrank.com'))
plus if you are using python3, you have to add the brackets to your print function, like you said:
print (e.utils.parseaddr('DOSHI <DOSHI#hackerrank.com>'))
print (e.utils.formataddr('DOSHI', 'DOSHI#hackerrank.com'))
Related
When I type the following code through command prompt in python, I get an error.
When I type the same in Atom and run via script, the code runs.
Can anyone advise what could be a problem here. Thanks.
Code:
city = 'kolkatta'
count = 0
for blah in city:
if blah=='l'
count=count+1
print (count)
You have to insert an empty line after count=count+1.
Continuation lines are needed when entering a multi-line construct. As
an example, take a look at this if statement:...
from 2.1.2. Interactive Mode
I am trying to create an "if" statement. But it comes back with an error. I have tried the same statement in pycharm and it works.
I use jupyter notebook so that it tells my any errors every line. I don't know what I'm doing wrong please help.
staffid1 = input('Input your Staff id here ...')
staffid2 = input('Re-enter your staff id here to confirm...')
if staffid1 == staffid2:
print('correct, searching database')
else:
print('invalid Staff id')
print('Error #1')
is it something I'm doing.
error-
File "<ipython-input-17-76bd76303e16>", line 1
if staffid1 == Staffid2:
^
SyntaxError: unexpected EOF while parsing
thankyou in advance
Staffid2 is capitalised in your error, but not in your original code. It shouldn't cause this exact error but hard to know what's going on if there are differences like this.
As written, your initial code runs fine for me in Jupyter.
It would be useful if you post the whole code exactly as you are running it in Jupyter to avoid these types of issues if you are still having problems...
I'm having some difficulty with an error I keep getting when I try to convert some python2.7 code to python3. I know this code works in python2.7. However when I try to convert it to python3 using 2to3.py I receive this error:
RefactoringTool: Can't parse ProAnalysis.py: ParseError: bad input: type=5, value='\t', context=('', (1562,0))
After searching the internet I saw that some people said that added -p would solve the problem however when I added that I only get the new error:
RefactoringTool: Can't parse ProAnalysis.py: ParseError: bad input: type=3, value="'Only using alignments greater than %.2f id and %.2f coverage'", context=('', (52,10))
I don't know if seeing the original file would help however it is a few thousand lines long. Any help would be very must appreciated. Thank you!
-Rachel
The context value lists the line and column numbers, so you should check your code there (as suggested by Dilettant in the comments to the question).
Presumably you'll get a good idea of what can trip the parser of 2to3.
>>> e=searchengine.searcher('searchindex.db')
>>> e.getmatchrows('functional programming') select w0.urlid,w0.location,w1.location from wordlocation w0,wordlocation w1 where w0.urlid=w1.urlid and w0.wordid=10 and w1.wordid=17
SyntaxError: invalid syntax
# it highlights the word select in the program
How do I correct this syntax error of the select statement? I am using Python with sqlite3.
You can't just plonk SQL into a python file. This error message is python telling you "I have no idea what select is".
To be more helpful, you'd need to share what library you got "searchengine" from, but no matter what that is, your code is not valid python, so there's no way it's going to work.
Here is the exercise I am working on: http://learnpythonthehardway.org/book/ex25.html
I am getting the following error message: "AttributeError: module object has no attribute 'print_last_word' even though I have defined it in my code(see below). I imported the relevant file using
import ex25
I'm calling it using
ex25.print_last_word(words)
All other functions are recognized.
However, dir(ex25) confirms what the error message says. I have saved the file and checked the spelling (though another set of eyes might help). Any help is appreciated.
def print_last_word(words):
"""Prints the last word after popping if off."""
word = words.pop(-1)
print word
The comment from DSM fixed it Specifically, it was
print ex25.__file__; you may not be importing the code you think you
are.
This showed me the filepath it was printing, which was not what I wanted. This also helped answer the question I posted at Learn Python the Hard way, exercise 25, not getting expected result
THANK YOU DSM