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.
Related
When the following code is executed line by line on shell, it is giving the expected output :
>>> name=input("Please give your full name : ")
Please give your full name : ins vikranth
>>> ListName=name.split(' ')
>>> outputString=ListName[1]+' '+ListName[0]
>>> print(outputString)
vikranth ins
The code is not running in total as a file but runs line by line on the shell.
The code is :
name=input("Please give your full name : ")
ListName=name.split(' ')
outputString=ListName[1]+' '+ListName[0]
print(outputString)
The error message is :
Please give your full name : ins vikranth
Traceback (most recent call last):
File "ReverseName.py", line 1, in <module>
name=input("Please give your full name
File "<string>", line 1
ins vikranth
^
SyntaxError: unexpected EOF while parsing
Why is this happening?
the reason why this is happening is your python version ... your IDLE is python 3.X while your file is being "translated" ( Interpret ) using python 2.X ... so there are 2 simple solutions:
1/ stick with python 3.X - your code is not going to change, just change Interpreter
2/ edit it to be compatible with python 2.X :
name=raw_input("Please give your full name : ")
also here are 2 online compilers, where you can see the difference:
Python 3.7 -> https://www.onlinegdb.com/online_python_compiler
Python 2.7 -> https://repl.it/languages/python
#Babu brother code seems to okey for me but you might forget somepoint.If person have a middle name it will cause an error. You have 2 array locations when user types more than 2 words it cause an unexpected end of file due to lack of location like Kevin Prince Boateng a footballer. You might want to look this https://docs.python.org/2/c-api/memory.html for a dynamical memory management
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.
I'm trying to solve a problem using the following code:
X,Y = map(float, input().split())
if X < Y and not X % 5:
print(Y - X - 0.50)
else:
print(Y)
This code gives me the desired output when I run using IDLE. However, when I try running this code using an interpreter provided by a competitive programming website, I get the following error:
Traceback (most recent call last):
File "./prog.py", line 1, in <module>
EOFError: EOF when reading a line
I tried reading the answers of other similar questions, but none of them seemed to work in my case.
I am not sure of the reason but the program is trying to read after the end of the data. You can solve the problem by exception handling
try:
data = input()
except EOFError:
break
Take another look at the codechef page. Notice the checkbox marked 'Custom Input'. With that checked/ticked a textbox will open where you can put your input lines.
The competitive programming website is likely running python 2. Python 2 treats input() differently than python 3.
You should rather use raw_input() than input().
From the docs:
raw_input() reads a line from input, converts it to a string
(stripping a trailing newline), and returns that.
Your problem can be explained from what was explained here:
In Python 2, raw_input() returns a string, and input() tries to run
the input as a Python expression.
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
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:)