Python 3 Jupyter Notebook "if" statement - 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...

Related

Short for loop for fetching bash users

A short python script i wrote to fetch users who are using bash. below is script. I don't know why its not working. Please share your ideas as to how this program is working inside.I am beginner in python and looking for lectures on data structures and algorithms. any answers are welcome. Thanks
f = open("/etc/passwd")
mainshell = '/bin/bash'
for line in f:
field = line.split(:)
shell = field[-1]
user = field[0]
if shell = mainshell:
print(user)
edit: I am getting no output. I tried to fetch values of variable shell and users and that exactly what i need but somehow if block is not working. Its not giving any error but just not working.
You are almost there with your script. But there are 2 reasons why its not working.
Your if statement is not doing a string comparison. You should be using == not =
You compare the string mainshell with shell, but you assume shell does not contain whitespaces. But the string will probably look like this /bin/bash (notice the whitespaces at the end?). This can be removed with shell = field[-1].strip()
I think then your program should work fine :)

How do you avoid errors do an automatic grading script?

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.

"//Print a message" error in pycharm

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.

Python - Code Query

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

Having trouble playing music using IPython

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!')

Categories