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
Related
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 :)
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'))
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 not sure if function is the word I am looking for. In fact I don't really know what I'm saying but I have some code and it's not quite doing it what I want to. Basically I want to copy and paste this code I've got and email it to someone. I want them to be able to simply copy and paste it into their Terminal and perform calculations.:
## SCSAC.py
def round(x, base=5):
return int(base * round(float(x)/base))
option = 'yes'
while (option == 'yes'):
x=float(raw_input('How many accumulated orders do you have from retailers: '));
y=float(raw_input('How many units are in the inventory: '));
z=float(raw_input('How many accumulated orders have you placed: '));
print 'Place an order of %s units' % round(((x / 25 + y / 10 + z / 25) + 115));
print ;
option=raw_input("Do you wish to calculate another order? (Enter 'yes' to continue or any other key to quit):: ");
print
Whenever I type this code in line for line, it works perfectly. That's because there are basically 3 seperate things happening here.
I define "round" which rounds a value to the nearest 5.
I define an option to loop the program upon completion
I define the actual program, and in that I round the answer and conclude with the option to loop. You may notice 2 print's that don't print anything, but they are only there to have blank lines.
When I copy and paste it, I get a syntax error.
I am not a programmer and I have just been playing with this all day. I just want to know how I can edit this code so it is copy/paste-able and will run the way it is supposed to.
Try using IPython instead of the regular Python interpret at the shell. With IPython, you can type %cpaste, and then paste a whole chunk of code, which it will execute for you step by step, saving the intermediate variables into working memory.
If you insist on pasting it in the regular interpreter, then do it line by line, and take special care for the indentations. The indentations are usually where paste syntax errors come from.
Even better, use Emacs. Then you can just save the pasted code into a file, like test.py, type M-x shell, and then python test.py to quickly run it. Or, if you saved it to a file like test.py then in IPython you can also type %run "test.py" and it will run the code and again store intermediate variables into working memory.
If you're copy/pasting this after making changes more than once or twice, just save it to a file and run it like a script.
You can save the code in a file, and run it using Python.
You can add #!/usr/bin/env python to the beginning of the file, so it can run on *nix systems (if you have execute permission).
Or, you can do python SCSAC.py and run your code. This works on all systems (AFAIK). You can email the file to your person, and she can run it using Python. This seems to be the easiest way to do it.
what I want to do is to write a script wich get some files as input and then run some command on them! I first want to know how many files dose the user has and then ask for him/her to upload it and later I want to run some command on it.
So I had used this command: for cnt inseq 19 2 31 in a similar situation long time ago but now when I use this command I get a syntex error!
this is my comand:
readnumber= raw_input("How many pares do you have?" )
for cnt in `seq 1 1 int(readnumber)`; do
pare$cnt-a = raw_input("filename of pare$cnt-a: ")
pare$cnt-b = raw_input("filename of pare$cnt-b: ")
cmd = "samtools merge pare$cnt.bam pare$cnt-a pare$cnt-b"
And this is the error that I get:
> for cnt in 'seq 1 1 int(readnumber)'; do
> ^
> SyntaxError: invalid syntax
so what I expext is when you put the readnumber=3 then it ask you to upload (or input) this:
pare1-a:
pare1-b:
pare2-a:
pare2-b:
pare3-a:
pare3-b:
and then run my command on it!
You probably want something like this:
pairs = int(raw_input("How many pairs do you have?"))
for pair_n in xrange(1, pairs + 1):
file1 = raw_input("pair%d-a: " % pair_n)
file2 = raw_input("pair%d-b: " % pair_n)
cmd = "samtools merge pair%d.bam '%s' '%s'" % (pair_n, file1, file2)
# os.system(cmd) to run the command.
You seem to be mixing up Python with bash scripting. raw_input is Python, but everything else in the script is bash.
Rather than trying to rewrite the whole thing in Python, it seems like you should just use bash, maybe with the read command instead of raw_input.
You are trying to use bash syntax in a python program. They are different languages. For instance, the seq command should instead be an xrange(...) command. You are also using bash variable syntax $somevar instead of python's variable syntax.
So either use bash or use Python, but don't try to mix-n-match.
And you may want to consider having the user provide the filenames on the commandline, rather than prompting the user for them. That will make your script more useful as a building block later.
The for-line of yours it not Python-code, it's a for-statement used in bash-shells.
What you want to do is to rewrite the for-line into this:
for cnt in range(1, readnumber):
Also, you will have to rewrite the other lines as well, since they are bash-syntax too.