LPTHW EX25 - not running normally in powershell - python

I am following zed shaw's book LPTHW and stuck on ex25. I have typed the code accordingly without errors. If I understand it right he has asked to run it first normal in powershell but when I put the command:
C:/mystuff> python ex25.py ,
It enters to next line without any output.
I tried importing ex25 in python interpreter this is the error i get:
>>import ex25
>>sentence = "All good things come to those who wait."
>>words = ex25.break_words(sentence)
After this I get an error:
Traceback <most recent call last>:
file stdin in line 1 <module>
file ex25.py line 3 in break_words
words = stuff.split(' ')
Value error: empty separator.
what am I doing wrong? Also i experimented for over half hour now trying different solutions , make it work. I guess the powershell wont respond because the program is basically a bunch of functions , without any input. But in the interpreter we do give an input of a sentence , then why the error? Doing my head-in.
the code link for LPTHW: http://learnpythonthehardway.org/book/ex25.html

I don't know your code for ex25 but here is mine for the line it says you have error on
def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words
It works for me you can make a new file and put that portion of your code and mine then compare them if they are exactly the same with 0 difference then tell me. if you can can you post that part of your code in the question

Related

Python3 EOFError: EOF when reading a line Hackerrank, or any online portal

I always get this error whenever using input() in Python3 in any online compiler, hackerrank, wipro portal, interviewbit etc. I've seen so many posts regarding this, but none of them is working for me. try except block leads to always execution of the except block which I don't want as I'm still not able to read any input.
Even as simple as the following code doesn't work. Help.
b = int(input())
print (b)
I get the following error:
Traceback (most recent call last):
File "main.py", line 227, in
Z = obj.solve(A)
File "/tmp/judge/solution.py", line 9, in solve
b = int(input())
EOFError: EOF when reading a line
Try going to https://www.hackerrank.com/challenges/python-loops/problem where the single input line is already there in the starter code for you. If you select Python 3 from the language dropdown, and then -- without entering any code of your own at all -- click Run Code, you should get a Wrong Answer "no response on stdout" response. Do you get that, or do you still get an EOFError? I'm assuming you don't get the EOFError and that perhaps there's a problem with where/how you're entering your code into their editor.
If you're getting an error like that with InterviewBit, I'd say it's because with InterviewBit you're not supposed to be reading from stdin at all -- the starter code will have a function that their test cases call, and then you complete the function code to return the desired output.

Python reading a file line by line and sending it to another Python script

Good day.
Today i was trying to practice python and Im trying to make a script that reads the lines from a file containing only numbers and using said numbers as a parameter in another Python script.
Here at work i sometimes need to execute a python script called Suspend.py, ever time i excute this scripit i must type the following information:
Suspend.py suspend telefoneNumber
I have to do this procedure many times during the day and i have to do this for every number on the list, it is usually a very long list. SO i though on trying to make things a little bit faster and creat a Python script myself.
Thing is a just started learning Python on my own i kinda suck at it, so i have no idea on how to do this.
In one file i have the following numbers:
87475899
87727856
87781681
87794922
87824499
88063188
88179211
88196532
88244043
88280924
88319531
88421427
88491113
I want python to be able to read line by line and send this number to another file script together with the word "suspend" on the previously said python script.
If I understand you correctly:
import subprocess
with open("file_with_numbers.txt") as f:
for line in f:
subprocess.call(["python", "Suspend.py", "suspend", line.strip()])

How to enter multiple input data using an online compiler such as ideone, codepad and compileonline?

I would like to test programs online, as the place where I work does not have a python compiler. There are many online sites such as ideone, codepad and complieonline. However, I have noticed that codepad does not accept input data and although ideone input data, it only accepts one entry data at a time. For example I wanted to test this program on complileonline
PREFIX = 'Simon says '
line = raw_input('Enter: ')
while line:
if line.startswith(PREFIX):
print line[len(PREFIX):]
line = raw_input('Enter: ')
I entered my input and separated them by pipeline (as stated on the website instructions) but kept getting this error message
Executing the code....
$python /tmp/135731949523855.py
Enter: Enter: Enter: Traceback (most recent call last):
File "/tmp/135731949523855.py", line 9, in ?
line = raw_input('Enter: ')
EOFError: EOF when reading a line
How can I enter multiple entries to test my program? How can I enter multiple entries onto these online websites, otherwise is there another, example testing with my code without the requirement of user input.
Well, consider to use hard-coded inputs inside your code (turn the "raw_input" function into something you except to get from the user). It can be great solution.
One of the options you have which is pretty close to raw_input, is to use sys.argv:
import sys
print sys.argv[1]
Compileonline provides you the option, below of your code, to add command line arguments.
However, on PythonAnywhere.com your code seems to run just fine :) You might want to try it there.
Good luck:)

Traceback NameError in python tutorial

I'm reading an online python tutorial book from here. The code is listed below. When I execute the code, I can type words into it but then it gave me the error below. What is the wrong with the code?
On a related note, if you have a better resource for leaning python, please let me know. I'm looking for one that is online and updated often (ex: railstutorial.org). The resource I am using have plenty of errors even this early in the book. Thanks.
Enter something : programmig is fun
Traceback (most recent call last):
File "break.py", line 5, in <module>
s = input('Enter something : ')
File "<string>", line 1, in <module>
NameError: name 'programmig' is not defined
#!/usr/bin/python
# Filename: break.py
while True:
s = input('Enter something : ')
if s == 'quit':
break
print('Length of the string is', len(s))
print('Done')
This is python 3 code. It seems like you're running it with python 2.
Run python --version to check which version of python you are using.
input() doesn't get a string, so it thinks that programmig is a variable. You can type the input you want in quotes to solve this.
A better way however, is to use raw_input, which returns a string.
So either do Enter something : 'programmig is fun', not recommended, or do s = raw_input('Enter something : ') recommended way
The cause of the confusion is that the book is probably for python 3, which has a different input, and also a different print, while you are using python 2.x.

how can I input a file and run an asynchronously command on it in python?

I'm trying to write a script that asks for an input file and then runs some command on it. when I run the script it askes me for filename and when I give the file (e.g example.bam) then I get this error:
NameError: name 'example.bam' is not defined
I tried many things but I couldn't fix it. Can someone tell me what is wrong?
This is my comand:
from subprocess import call
filename = input ("filename: ");
with open (filename, "r") as a:
for command in ("samtools tview 'a' /chicken/chick_build2.1_unmasked.fa",):
call(command, shell=True)
This is a short version of my command: it has to do much more stuff. I'm also thinking to input 4-6 files at same time (perhaps this information is helpful to clarify my intentions).
input is equivalent to eval(raw_input(prompt)). So what your script currently tries to do is interpret your input ("example", in your case), and execute as if it were a statement in your script. For user input (and might I simply say "for any input" -- unless you know what you're doing), always use the raw_input function.
So, to solve it, replace input with raw_input:
filename = raw_input("filename: ")

Categories

Resources