Python logical NOT operator for regular expressions - python

I tried searching for this answer online but I haven't found any luck. I am wondering if python supports a logical not operator (usually '!') in other languages for an if statement, or any control statement really. For example, I am looking to achieve this functionality.
if !(re.search('[0-9]', userInputVariable):
fix error and report to user
...Proceed with rest of code...
Where basically if the user does not input a number I correct it to a default value and proceed with script
Thanks!

You're looking for the not operator.
But that's not how you check for a number.
try:
int(userInputVariable)
except ValueError:
print "Not a number"
...
if not userInputVariable.isdigit():

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")])

What is the difference between using eval(*code*) and just *code*?

What is the difference between:
cerebro.addsizer(bt.sizers.FixedSize, **eval('dict(' + args.sizer + ')'))
and:
cerebro.addsizer(bt.sizers.FixedSize, dict(args.sizer))?
I ran the first one and it worked but the second one causes an error.
Can someone please help me?
Python’s eval() allows you to evaluate arbitrary Python expressions from a string-based or compiled-code-based input. This function can be handy when you’re trying to dynamically evaluate Python expressions from any input that comes as a string or a compiled code object.

Python3 NZEC Error

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.

In python, is there anyway to input a string without quotation marks?

I want user to input a name of a table, table = str(input("table: ")) works, but it's kind of annoying to put 'name' everytime instead of just name, is there any work around for this?
Use raw_input:
table = raw_input("table: ")
>input([prompt])
Equivalent to eval(raw_input(prompt))
This function does not catch user errors. If the input is not syntactically valid, a SyntaxError will be raised. Other exceptions may be raised if there is an error during evaluation.
If the readline module was loaded, then input() will use it to provide elaborate line editing and history features.
Consider using the raw_input() function for general input from users.

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.

Categories