Script works in shell but not as IDLE file - python

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

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.

how to desactivate double indent when I paste the code in a python terminal

I try to understand why my pasting in my terminal (linux) in my python interpreter put double indent every line for example :
if I paste these lines :
def function(string):
final = ''
for i in string:
try:
final += str(int(i))
except ValueError:
return int(final)
I'll get this in my terminal :
ent>>> def function(string):
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
>>> final = ''
File "<stdin>", line 1
final = ''
^
IndentationError: unexpected indent
>>>
>>> for i in string:
File "<stdin>", line 1
for i in string:
^
IndentationError: unexpected indent
>>>
And more and more... because when there is a for or any instruction that needs a tab, it return it directly instead to wait for the next instruction,so it rise an IndentationError:
And I realize that if I paste this for exemple :
import mathimport threading
it will paste like this in my terminal :
>>> import math
>>> import threading
>>>
I didn't have from the beginin, so any idea ? because it was impossible to find my answer in internet..
They are mixed, if we see my first code, it's only one indentation, and then the second line is one tab and one indentation, and from the fourth line it's two tabs, as simply as we see on my question.
And if I paste it in any other text editor it work perfectly, I tried to see every setting in my terminal or in my linux but I've found nothing so far... It's really strange !
Edit :
Apparently for a reason that I don't know, it come from Atom the editor that I use for writing my python code.
I explain it's very strange !
if I copy the code from Atom, and I paste it in any other text editor (there, in kate or other) and I copy from this other editor and paste in my terminal, it's happend again, double indendation.
But to correct that I just create a new fil in Atom, and then is fine, It work properly the pasting in the terminal.
Question solved, but why ?? That's a mystery ^^

LPTHW EX25 - not running normally in powershell

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

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.

Python 3.1 and Sublime Text 2 error

I want to use a SublimeText2 as IDE for developing applications in Python.
I have the following problem :
When I make a new file, save it as a python and do Tools -> Build System -> Python
CTRL + B
I get this error:
Please type your name and press enter: Traceback (most recent call last):
File "/Users/strielok/Desktop/hello.py", line 1, in <module>
personsname = raw_input("Please type your name and press enter: ")
EOFError: EOF when reading a line
[Finished]
Here is the code of the program:
personsname = raw_input("Please type your name and press enter: ")
print "Hello " +personsname
However when I run this code from terminal (I am on Mac), it works perfectly.
Any idea what the problem might be ?
Install sublimeREPL and then choose Tools->sublimerepl->python.
Reference:http://gimo.me/sublime-text2-skills/
Taken from Sublime Text's forums, seems that stdin will not be connected with anything so it's expected.
Source: http://www.sublimetext.com/forum/viewtopic.php?f=3&t=1519&p=6908&hilit=python+input#wrap
I've written a plugin which allows builds in sublime text to take input. It's a bit rough around the edges, but it works on my machine.

Categories

Resources