Exact output was coming but some error was raising - python

In the above Python code the exact output was coming but after output they were showing some error
def main():
i=1
while i < 3:
inputString = input()
inputStringTwo = input()
inputStringThree = input()
outputString=inputString[0] + inputStringTwo[0] + inputStringThree[0]
print(outputString)
i+=1
main()
Here we are giving input as
Very
Important
Person
The Error was
VIP
Traceback (most recent call last):
File "main.py", line 15, in <module>
main()
File "main.py", line 5, in main
inputString = input()
EOFError: EOF when reading a line
Here VIP is the output
For Question and test cases please refer below link
https://drive.google.com/file/d/1cXmIW56uurB83V4FNRuNw9VhJ7RcYU9j/view?usp=sharing

if you are using an online IDE;
EOFError is raised when one of the built-in functions input() or raw_input() hits an end-of-file condition (EOF) without reading any data. This error is sometimes experienced while using online IDEs. (https://www.geeksforgeeks.org/handling-eoferror-exception-in-python/)
try not to use online IDEs its better to download python

Related

How to fix runtime error in online editor?

every time i upload my code in online editor i get runtime error since i can run it without any problem offline ( i mean in my vs code) i think it's for taking input, please help me here is my code
mylist=[]
n=int(input())
x=int(input())
k=int(input())
for i in range(0,n):
a=input()
mylist.append(a)
bb=x+k
bc=bb%n
print(mylist[bc-1])
i get this message
Traceback (most recent call last):
File "/home/3ea91305ad98bd23bfc532efbb96d2d8.py", line 3, in <module>
x=int(input())
EOFError: EOF when reading a line

(Python 3.x)Syntax Error EOF while parsing when reading a line from a file.

(Python 3.x)So I keep getting a syntax error when reading a file. I've had this problem for a while now. I've been working on other parts of the program so far(not shown), but can't solve this syntax error. I am very confused.
I feel guilty posting about a syntax error but I am out of ideas.
Here is the error: Syntax Error: unexpected EOF while parsing: , line 0, pos 0 # line 8
Code:
def main():
filename = 'p4input.txt'
infile = open(filename, "r")
command = 0
while command != 3 and command < 3:
command = eval(infile.readline()) #Problem here
convert = eval(infile.readline())
print(command)
print(convert)
print("done")
main()
The input file (p4input.txt)
Has the following data:
2
534
1
1101
Complete traceback:
Traceback (most recent call last):
File "C:/Users/Ambrin/Desktop/CS 115/TESTER.py", line 16, in <module>
main()
File "C:/Users/Ambrin/Desktop/CS 115/TESTER.py", line 8, in <module>
command = eval(infile.readline())
File "<string>", line 0, in ?
Syntax Error: unexpected EOF while parsing: <string>, line 0, pos 0
This is happening because when you get to the end of the file, readline() returns an empty string, so you're doing eval(''). You need to check for an empty string and break.
As pointed out in a comment above, you probably shouldn't be using eval. If all your inputs are expected to be integers, you can just use int() instead. You'll still need to check for '' though.

EOFError when executing Python script using stream redirection

I keep getting the EOF error when I execute the Python script using stream redirection.
Script
name=input('Enter your name : ')
print ('Welcome ' + name)
input('Press \'ENTER\' to exit!')
Execution command:
helloworld.py < input.dat
Error:
Enter your name : Welcome Gunit
Press 'ENTER' to exit!Traceback (most recent call last):
File "D:\Reference\Python\Codes\helloworld.py", line 28, in <module>
input('Press \'ENTER\' to exit!')
EOFError: EOF when reading a line
Your code reads two lines from the file (one for the name, and one for the "Enter to exit"). Your input file only has one line in it.
Therefore, Python reaches the End-Of-File indication before it can read the 2nd line.
To fix, ensure that your input.dat has at least two lines, or delete the 2nd input() call.

Python 3 [TypeError: 'str' object cannot be interpreted as an integer] when working with sockets

I run the script with python3 in the terminal but when I reach a certain point in it, I get the following error:
Traceback (most recent call last):
File "client.py", line 50, in <module>
client()
File "client.py", line 45, in client
s.send(bytes((msg, 'utf-8')))
TypeError: 'str' object cannot be interpreted as an integer
This is the code it refers to.
else :
# user entered a message
msg = sys.stdin.readline()
s.send(bytes((msg, 'utf-8')))
sys.stdout.write(bytes('[Me] '))
sys.stdout.flush()
I read the official documentation for bytes() and another source
https://docs.python.org/3.1/library/functions.html#bytes
http://www.pythoncentral.io/encoding-and-decoding-strings-in-python-3-x/
but I am no closer to understanding how to fix this. I realise that my msg is a string and I need an integer, but I am confused about how to convert it. Can you please help me, or direct me to a source that will help me?
Edit 1: I changed the line
s.send(bytes((msg, 'utf-8')))
to
s.send(bytes(msg, 'utf-8'))
but now I get the following error:
Traceback (most recent call last):
File "client.py", line 50, in <module>
client()
File "client.py", line 46, in client
sys.stdout.write(bytes('[Me] '))
TypeError: string argument without an encoding
Edit 2: According to #falsetru updated answer.
Using bytes literal gives me
TypeError: must be str, not bytes
Change the following line:
s.send(bytes((msg, 'utf-8')))
as:
s.send(bytes(msg, 'utf-8'))
In other words, pass a string and an encoding name instead of a passing a tuple to bytes.
UPDATE accoridng to question change:
You need to pass a string to sys.stdout.write. Simply pass a string literal:
sys.stdout.write('[Me] ')

While function python

Hello is use eclipse and pydev, I was wondering why my sample code won't work trying to use the while function.
print("Welcome to the annoying program")
response = ""
while response != "Because.":
response = input("why\n")
print("Oh,ok")
the output is the following:
Welcome to the annoying program
why
Because.
Traceback (most recent call last):
File "/Users/calebmatthias/Document/workspace/de.vogella.python.first/simpprogram.py", l ine 9, in <module>
response = input("why\n")
File "/Users/calebmatthias/Desktop/eclipse 2/plugins/org.python.pydev_2.2.3.2011100616/PySrc/pydev_sitecustomize/sitecustomize.py", line 210, in input
return eval(raw_input(prompt))
File "<string>", line 1
Because.
^
SyntaxError: unexpected EOF while parsing
The input function in 2.x evaluates the input as Python code! Use the raw_input function instead.
With input, your text "Because." is being evaluated, and it's a syntax error because the dot isn't followed by anything.

Categories