This is my second beginner python course, so I'm learning! For most things I'm working with Python notebooks because I feel it has more flexibility. The actual program is Coursera. I recently transferred the following code from python notebooks (that worked fine) back to Coursera, and I'm getting syntax errors. Not the first time. How do I avoid this? Looking for any advice.
"TabError: inconsistent use of tabs and spaces in indentation"
def email_list(domains):
emails = []
for provider, user in domains.items():
for each_user in user:
new_user=("{}#{} ".format(each_user,provider))
emails.append(new_user)
return(emails)
print(email_list({"gmail.com": ["clark.kent", "diana.prince", "peter.parker"], "yahoo.com": ["barbara.gordon", "jean.grey"], "hotmail.com": ["bruce.wayne"]}))
I have faced such problems in the past and here is what I did, after pasting your code in Coursera, try deleting every space and tab from your code and retype them for example, delete the spaces in the def line until you have this line
def email_list(domains):emails = []
after this line pressing return or enter right after the colon will give you the right indentation and hopefully, your code will run smoothly.
Related
I have a long time problem and it got me angry, so I will ask here. When I have a long function call in VS Code, it automatically enteres a new line somewhere in the parametres.
I have this on one line:
dict_file = os.path.join(path_converted,
'dictionary_'+filename+'.picklelongtestlongtestlongtestlongtestlongtest')
and it adds newLine behind "(path_converted, 'dictionary_' +" and the rest is written on the new line with some indention.
I have pretty big monitor, so I do not want to end these lines so early. Could I somehow turn it off in Settings? I am using FormatOnSave and it is pretty anoying.
Thank you for all answers :))
Andrew
There're the following two settings in VS Code:
"editor.wordWrap": Controls how lines should wrap.
"editor.wordWrapColumn": Controls the wrapping column of the editor when #editor.wordWrap# is wordWrapColumn or bounded.
So there're two solutions to your question, writing it in Settings.json:
Turn off the wordWrap:
"editor.wordWrap":"off",
Still keep wordWrap on but changing the code's max-length:
"editor.wordWrap": "wordWrapColumn",
"editor.wordWrapColumn":130,
I'm a student studying Python and Web Crawling in Korea.
I found something I can't understand why. I want to ask why this happens and how can I fix it.
It will lovely if someone is gonna help me.
Here is my situation:
This is a code for my web crawling. There is some Korean words, but that's not important, I think.
zeropay_official = 'https://www.zeropay.or.kr/main.do?pgmId=PGM0081'
driver = webdriver.Chrome('./driver/chromedriver')
driver.get(zeropay_official)
driver.find_element_by_id('tryCode').click()
driver.find_element_by_id('tryCode').send_keys('서울특별시')
driver.find_element_by_id('skkCode').click()
driver.find_element_by_id('skkCode').send_keys('노원구')
driver.find_element_by_id('pobsAfstrName').send_keys('다마식당')
driver.find_element_by_xpath('//*[#id="form"]/div[2]/a').click()
test = driver.find_element_by_id('list_div')
test.text
and right below this Jupyter Notebook cell, I put the last line of the code,
test.text
to check what's happening.
But, first cell's output ls ''(None), and second cell's output is some string which I wanted to get.
Why Is this happening? And if I need to get the output data string on the first cell, to make this code as a module so my team can import it, what should I do?
Check this image if you couldn't clearly understand what I said due to my poor English.(sob)
You can add some wait time.
zeropay_official = 'https://www.zeropay.or.kr/main.do?pgmId=PGM0081'
driver = webdriver.Chrome('./driver/chromedriver')
driver.get(zeropay_official)
driver.find_element_by_id('tryCode').click()
driver.find_element_by_id('tryCode').send_keys('서울특별시')
driver.find_element_by_id('skkCode').click()
driver.find_element_by_id('skkCode').send_keys('노원구')
driver.find_element_by_id('pobsAfstrName').send_keys('다마식당')
driver.find_element_by_xpath('//*[#id="form"]/div[2]/a').click()
time.sleep(time_in_seconds)
test = driver.find_element_by_id('list_div')
test.text
As Korean text is taking some time to appear.
This is the code in pyCharm app on Linux Ubuntu
#1/usr/bin/python
def main(msg):
//Print a Message
print(msg)
main("Hello People")
I keep getting an error.
This is the video course I am following:
https://www.youtube.com/watch?v=wBp0Rb-ZJak
At the 3:37:00 mark is where it's supposed to refresh, but because of my error it will not.
Here is a screenshot of the code and error.
http://s1227.photobucket.com/user/MrGHLover/media/Screenshot%20from%202017-09-10%2023-36-52.png.html?sort=3&o=0
It is the same in the video but I can't figure it out.
Commenting by '//' is not allowed in python. Use '#' for commenting.
You can comment a line in Python using #. Or you can (kind of) create a multi-line comment using '''<some long string>""".//` Isn't valid Python.
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 have the lines of code
import IPython
IPython.display.Audio(url="http://www.1happybirthday.com/PlaySong/Anna",embed=True,autoplay=True)
And I'm not really sure what's wrong. I am using try.jupyter.org to run my code, and this is within if statements. The notebook is also taking in user inputs and printing outputs. It gives no error, but just doesn't show up/start playing. I'm not really sure what's wrong.
Any help would be appreciated. Thanks!
First you should try it without the if statement. Just the two lines you mention above. This will still not work, because your URL does point to an HTML page instead of a sound file. In your case the correct URL would be 'https://s3-us-west-2.amazonaws.com/1hbcf/Anna.mp3'.
The Audio object which you are creating, will only be displayed if it is the last statement in a notebook cell. See my Python intro for details. If you want to use it within an if clause, you can use IPython.display.display() like this:
url = 'https://s3-us-west-2.amazonaws.com/1hbcf/Anna.mp3'
if 3 < 5:
IPython.display.display(IPython.display.Audio(url=url, autoplay=True))
else:
print('Hello!')