Python3 NZEC Error - python

This piece of code is supposed to find account balance after withdraw from bank(fee=0.5).
wd,ac=raw_input().split(" ")
wd=int(wd)
ac=float(ac)
if(ac<wd):
print(ac)
elif(wd%5==0):
if(ac>wd+0.50):
print(ac-wd-0.50)
else:
print(ac)
else:
print(ac)
I got a Runtime NZEC Error while submitting on codechef. I am newbie and had searched for resolve and had used split(" ") in place of int(input()), which I had tried previously, but the problem still occurs.
Geeksforgeeks says:
"In python, generally multiple inputs are separated by commas and we read them using input() or int(input()), but most of the online coding platforms while testing gives input separated by space and in those cases int(input()) is not able to read the input properly and shows error like NZEC"
Given that I've tried to account for that... What is wrong with my code?

It looks like an error with your raw_input statement. Remember that, in python 3, raw_input doesn't exist any more. If you changed it from raw_input to input, then the code works just fine.

Related

How can I customise error messages shown by PyInputPlus?

How can I customise error messages shown by PyInputPlus in python?
I have tried many method but unable to do it.
import pyinputplus as pyip
number = pyip.inputNum("Enter your phone number : ",
min=1000000000,
max=9999999999)
number
I want to print error message as please enter a valid 10 digit phone number.
Is there any way to so it?
I try to use "allowRegexes and blockRegexes" but unable to understand it.
If you are using this for a real world project I would recommend using input from python itself, this lib doesn't seem very well documented and mantained. This could bring a lot of weird errors in your code for the future.
But to answer your question, you could do it using regex with the parameter blockRegexes. If you were unable to understand it, this will be more a regex question than a python question.
From this website you can learn a lot about regex, that I recommend, regex is a very important tool to understand.
About your problem, accordingly to the docs:
blocklistRegexes (Sequence, None): A sequence of regex str or
(regex_str, error_msg_str) tuples that, if matched, will
explicitly fail validation.
So, in your case the first item in the tuple, should be a regex to block everything that have more or less than 10 integers characters:
^\d{10}$
The full explanation for this regex can be found here
The second item in your touple should be the string you want to appear when the error occurs:
"please enter a valid 10 digit phone number"
So your code would be like this:
number = pyip.inputNum("Enter your phone number : ",
min=1000000000,
max=9999999999,
blockRegexes=[(r"^\d{10}$","please enter a valid 10 digit phone number")])

Python3 : EOF Error while taking input on GUVI

This is my code in python3:
import heapq
myQueue = []
n = raw_input()
try:
num = int(n)
if num<=100000 :
arr = input().split()
for i in range(num):
heapq.heappush(myQueue, arr[i])
print(myQueue[0])
except (NameError, ValueError):
print("Wrong Input, N should be under 100000")
except IndexError:
print("Inputs is less than actually required")
except EOFError:
print ("Error: EOF or empty input!")
I am trying to implement priority queue.
But I am facing this EOF Error while solving this problem on GUVI.
Output:
Error: EOF or empty input!
The tried to catch the error using except EOFError, but that will just make my program run but does not solve input problem right.
I even tried to run this piece of code on Sublime text editor as well as Vs code,
where it runs just fine, Correct Output.
I don't understand, is there a problem in my code or That Online Platform.
I even tried to search the answer on their Q&A platform of GUVI, I found similar question but no one has answered it.
And this is not just for this piece of Code but I found the same error for many before.
Could ANYONE help me, Please!
Thanking you in advance.. :)
EOF error occurs if the input is not provided.
In case of most of the online compilers you need to provide the inputs before running the code.
With that said, while your are trying to access the input via raw_input() there will be no input supplied resulting in the above error.
To avoid this, error supply inputs before running your code like below
Also, I can notice that you are using raw_input() and input(). Please note that raw_input() can be used if you are using Python 2 and input() if you are using python 3 correspondingly.

im using a python book and I copied the code but it says, multiple statements found while compiling a single statement don't know what's wrong

name = input("Hi. What's your name? ")
print("name")
print("Hi,", name)
input("\n\nPress the enter key to exit.")
It says multiple statements found while compiling a single statement
I'm using a book and tried many different things. Also, I'm a noob at this as you can tell. If you have any suggestions that'll be great
Your code is fine, assuming you are using python 3, but you need to type (or paste) each line, one at a time. Based on what you are seeing, I suspect you are putting it all in at once, without a new line after each line.
If you are using python 2, you'll need to use raw_input rather than input, like this:
name = raw_input("Hi. What's your name? ")
print("name")
print("Hi,", name)
input("\n\nPress the enter key to exit.")
So couple of things:
print("name") will not print the name you captured from the last variable but a string that says name
print("Hi,", name) prints ('Hi,', 'Dmitry') which is probably not what you want, instead do this: ', '.join(["Hi", name]) There are probably other Python 3 conventions but I work in Python 2 so I don't know them all off the top of my head.
input("\n\nPress the enter key to exit.") not sure of the purpose of this line. Seems like a stray line from a block of code and it's not being assigned to any variable. Furthermore it throws an error SyntaxError. What book are you using if I may ask?
You can check the Python version in the HELP>about IDLE tab of IDLE editor or Shell - you seem to be using python 2 as others have stated
If you are able to enter and run one line of code at a time, followed by the next line then you are using the Shell, not the IDLE editor
You should be able to paste the code you have in question into IDLE editor and run (F5) - you should be prompted to save before it is run in the Shell.

Python 2.7: Handling wrong input which is not a string

I am writing a program which is supposed to contain a way of informing the user that the input for one of the variables is not a string, if entered as a name by user.
E.g. program expects a user input of any string, and if it is a string which is contained within dictionary, it will print out its value, if not, it will print out an error message.
ageofdeath.getage('JesusChrist')
33
ageofdeath.getage('John McCena')
This is not a bible character. Please enter a name of a character from the bible.
but, the program should at least throw an error message when confronted with wrong user input such as
ageofdeat.getage(JesusChrist)
ideally popping up a message along the lines of "This is not a string please input a string". Instead, no matter whether i try to use if = or isinstance, it always shows typical python name is not defined error. Is there a way of going round this or not really, as it is a default way of python shell handling the input?
Your program isn't even getting to the part where it executes your getage() method. It is failing far earlier.
You're using input() instead of raw_input(). Thus JesusChrist is taken as the name of a variable because input() evaluates what the user types as a Python expression. JesusChrist is a legal Python variable name, it just hasn't been defined, so Python tells you that. And because it knows you can't do anything with a value that doesn't exist, it stops.
Now you could catch that error by wrapping your input() in a try/except block, but that's just trying to compensate for making the wrong decision in the first place. The right answer is to use raw_input() to get input from your user and it will always be a string.

Python: EOFError: EOF when reading a line

This may be repeated, but none of the existing answers solved my problem.
So, I'm using Python 2.7, and I get this error (title) whenever I try this:
number = int(raw_input('Number : '))
I tried this in Sublime Text 2, compileronline.com and in codecademy; it fails in the first 2 of this sites. It works on codecademy and in the terminal compiler, but I can't understand exactly why it is failing.
The issue here is that Sublime text 2's console doesn't support input.
To fix this issue, you can install a package called SublimeREPL. SublimeREPL provides a Python interpreter that takes in input.
And as for compileronline.com, you need to provide input in the "STDIN Input" field on the lower right of the website.
try:
value = raw_input()
do_stuff(value) # next line was found
except (EOFError):
break #end of file reached
This seems to be proper usage of raw_input when dealing with the end of the stream of input from piped input.
Refer this post

Categories