I have been trying to add some validation for users entering a new word to a text file.
The input must consists of letters only and I have got this working using if statements with .isalpha(), however I wanted to try and see if I could get it working using try, except and so far I have not got it working.
The try statement is allowing all input in no matter if it contains digits or spaces. I cant seem to spot where i've gone wrong.
def AddNewWords():
List = []
Exit = False
while not Exit:
choice = input("Please enter a word to be added to the text file: ")
try:
choice.isalpha()
except:
print("Not a valid word")
continue
else:
List.append(choice)
Exit = True
Return List
AddNewWords()
isalpha() returns True/False, it doesn't raise any exception.
Try this instead:
choice = input("Please enter a word to be added to the text file: ")
if not choice.isalpha():
print("Not a valid word")
continue
List.append(choice)
Exit = True
FWIW, you can also rewrite your loop in a more compact way without using the exit variable, but rather while True + break:
while True:
choice = input("Please enter a word to be added to the text file: ")
if choice.isalpha():
List.append(choice)
break
print("Not a valid word")
There are many way to achieve your result without a try / except clause. However, raising a manual exception is a perfectly valid approach and can be applied with only a couple of changes to your code.
First, you need to ensure a False result for str.isalpha raises an error:
if not choice.isalpha():
raise ValueError
Second, you should define explicitly the exception you are catching:
except ValueError:
print("Not a valid word")
continue
Complete solution:
def AddNewWords():
L = []
Exit = False
while not Exit:
choice = input("Please enter a word to be added to the text file: ")
try:
if not choice.isalpha():
raise ValueError
except ValueError:
print("Not a valid word")
continue
else:
L.append(choice)
Exit = True
return L
AddNewWords()
you need to check if true/false not try the except since you wont get any exception. the code should be.
def AddNewWords():
List = []
Exit = False
while not Exit:
choice = input("Please enter a word to be added to the text file: ")
if not choice.isalpha():
print("Not a valid word")
continue
else:
List.append(choice)
Exit = True
return List
AddNewWords()
Related
def aboveIntegerInput(output_message="Enter your number: ", error_1="Please enter a number above {}!", error_2="Integers only!(Please do not leave this blank)", above=0):
while True:
try:
user_input = int(input(output_message))
if user_input >= above:
return int(user_input)
break
else:
print(error_1.format(above))
except ValueError:
print(error_2)
As you can see here the code is supposed to check if an input is an integer and it is above a certain value which by default is 0, but could be changed.
When the user inputs random letters and symbols it see that there is a value error and returns "Integers only!(Please do not leave this blank)".
I want to be able to check if the user inputs nothing, and in that case only it should output "This is blank/empty", the current way of dealing with this is to not check at all and just say "Integers only!(Please do not leave this blank)", in case there us a value error. I want to be able to be more specific and not just spit all the reasons at once. Can anyone please help me?
Thanks in advance.
You could do something like this :
def aboveIntegerInput(output_message="Enter your number: ", error_1="Please enter a number above {}!", error_2="Integers only!", above=0, error_3="Please do not leave this blank"):
while True:
user_input = input(output_message)
try:
user_input = int(user_input)
if user_input >= above:
return user_input
break
else:
print(error_1.format(above))
except ValueError:
if(not user_input):
print(error_3)
else:
print(error_2)
I moved the input outside the try/except block to be able to use it in the except ! This worked fine for me, I hope this is what you needed.
You could just break the input and the conversion to int into two steps, like this:
def aboveIntegerInput(output_message="Enter your number: ", error_1="Please enter a number above {}!", error_2="Integers only!(Please do not leave this blank)", above=0):
while True:
try:
user_input = input(output_message)
if not user_input:
print("Please do not leave this blank")
continue
user_input = int(user_input)
if user_input >= above:
return int(user_input)
break
else:
print(error_1.format(above))
except ValueError:
print(error_2)
I have a problem in which users can input spaces or nothing and still pass through the program, how do I go about preventing this? I am still a beginner at python.
def orderFunction(): # The function which allows the customer to choose delivery or pickup
global deliveryPickup
deliveryPickup = input("Please input delivery or pickup: d for delivery p for pickup")
if deliveryPickup == "d":
global customerName
while True:
try:
customerName = (input("Please input your name"))
if customerName == (""):
print("Please input a valid name")
else:
break
global customerAddress
while True:
try:
customerAddress = (input("Please input your name"))
if customerAddress == (""):
print("Please input a valid Address")
else:
break
global customerPhnum
while True:
try:
customerPhnum = int(input("Please input your phone number"))
except ValueError:
print("Please input a valid phone number")
else:
break
print("There will also be a $3 delivery surcharge")
elif deliveryPickup == "p":
customerName = (input("Please input your name"))
if customerName == (""):
print("Please input a valid name")
orderFunction()
else:
print("Please ensure that you have chosen d for Delivery or p for Pickup")
orderFunction()
orderFunction()
Here is my attempt at doing this but I get all kinds of unindent and indent errors at the moment and I think my while loops are probably wrong.
Essentially if I input a space or hit enter into one of the customer inputs (customerName for instance) it gets stored. This needs to prevented and I have tried to fix it by using while loops which obviously haven't worked.
Hopefully someone has a solution to this problem
Many Thanks.
.strip() removes all tabs or spaces before and after a string.
Meaning all spaces == empty string. All tabs == empty string. So all you have to check if the length of that string != 0 or the string is not empty. Just use an infinite loop to keep on forcing the right input.
Also as a tip, you don't have to limit yourself into one function.
Here's a working code below.
def getNonBlankInput(message, error_message):
x = input(message)
while len(x.strip()) == 0:
x = input(error_message)
return x
def getValidIntegerInput(message, error_message):
msg = message
while(True):
try:
x = int(input(msg))
break
except ValueError:
msg = error_message
return x
def orderFunction(): # The function which allows the customer to choose delivery or pickup
global deliveryPickup
global customerName
global customerAddress
global customerPhnum
deliveryPickup = input("Please input delivery or pickup: d for delivery p for pickup")
if deliveryPickup == "d":
customerName = getNonBlankInput("Please input your name: ", "Please input a valid name: ")
customerAddress = getNonBlankInput("Please input your address: ", "Please input a valid address: ")
customerPhnum = getValidIntegerInput("Please input your phone number: ", "Please input a valid phone number: ")
print("There will also be a $3 delivery surcharge")
elif deliveryPickup == "p":
customerName = getNonBlankInput("Please input your name: ", "Please input a valid name: ")
else:
print("Please ensure that you have chosen d for Delivery or p for Pickup")
orderFunction()
orderFunction()
Try using a regular expression that checks if any character between "A-Z" has been inserted, if not, give an error
The while loops are a decent solution, you just need to add more checks to your if statements.
First, you don't need a try statement on the top two loops. Don't use a try statement unless you're expecting an error, which you need to handle with an except statement, like you do in the bottom while loop.
Then you just need to add more conditions to your top two loops, I don't know exactly what you want to prevent, but you could try checking the length of the input, also see this answer for an interesting method:
https://stackoverflow.com/a/2405300/8201979
Instead of using input right away you can make a function similar to this one that will only allow valid inputs.
You can use this valid_input function instead of input.
def valid_input(text):
not_valid = True
res = ''
while not_valid:
res = input(text)
if res.split(): # if text is empty or only spaces, this creates an empty list evaluated at False
not_valid = False
return res
here the check is pretty simple: every text made out of nothing or spaces won't be allowed and we will keep asking for the same input until a valid information is given.
I made this code simple just so you get a general idea. But you can change the validation test to your liking and maybe also output a warning saying why the input wasn't allowed so the person knows what to do. You can do more advanced validation with regex, and maybe you need a minimum text length etc...
You have indent error because you have a try statement without the corresponding except.
You need both to make it work (as you did in the Phone number section).
Here is a link to the try/except: docs
Also, you can check if a string is empty as detailed in this answer.
So for example you want to write:
try:
customerName = input("Please input your name")
if not customerName:
print("Please input a valid name")
else:
break
except ValueError:
print("Please input a valid name")
Although the above seems a bit redundant, so you might want to raise an exception if the customer name is empty, catch the exception in the except block, print the warning and return error (or something else).
try:
customerName = input("Please input your name")
if not customerName:
raise ValueError
except ValueError:
print("Please input a valid name")
else:
break
Try adding another while true for pick and delivery option so that it can prevent taking other inputs
you don't need any of those try/excepts (which are broken anyway).
Its difficult to figure out what you're trying to do, are you trying to raise an exception if an empty string is passed, or request another input from the user? You seem to be half implementing both at the moment.
If its the latter, something like this would work.
def func(fieldname):
while True:
val = input("Please input your {}".format(fieldname))
if val.strip() != "":
break
else:
print("Please input a valid {}".format(fieldname))
return val
delivery_pickup = input("Please input delivery or pickup: d for delivery p for pickup")
if delivery_pickup == "d":
customer_name = func("name")
address = func("address")
phone_number = func("phone number")
What you are looking for is the str.strip method that remove trailing whitespace in strings.
Also I think try is not particularly suited for your needs here.
customerName = input("Please input your name")
while not customerName.strip():
customerName = input("Please input a valid name")
for the phone number I would not convert to integer because if the phone number starts with zeros, they will not be stored.
If I'm trying to prompt a user for their street name and they enter a # symbol i want to return an error and loop back to the top until they have entered a valid name.
Code:
def streetname():
while True:
try:
nameinput = str(input("Please enter a street name: "))
except Exception: # want to print error == if nameinput.find('#') > -1:
print("Error: name contains #, please try again")
continue
break
return nameinput
Goal of code: I want to prompt user for street name until they enter a valid name. If it contains '#', print error and try again. if it's valid, return nameinput.
I'm not quite sure how to use try and except with if statements
You probably shouldn't use try...except for such a simple input check, that can easily done with if alone.
def streetname():
while True:
nameinput = input("Please enter a street name: ")
if "#" in nameinput:
print("Error: name contains #, please try again")
continue
return nameinput
If you find a '#' in the string simply restart the loop, otherwise just return the name out of the function.
Also input already returns a str in Python 3, so there's no need to convert it. In Python 2 you should use raw_input instead which will also return a str.
First off, you're using a python's general Exception class. A string with a pound sign in it wouldn't trigger any of python's native exceptions, you have to write your own exception class and then call it.. or you can do something like this
if '#' in nameinput: #pound sign was found in the string
#either raise an Exception
raise('Can not use pound sign') # though this will probably break the while loop
# or
print 'can not use pound sign'
continue # to go back to the top of the while loop and prompt the user again
I am writing a python program in which I want to allow the user to enter only 10 numbers from the terminal. If they try to enter more than 10 it should prompt that they can't enter more than 10 numbers. How can I achieve this in python?
My code looks like as follows:
def get_list():
'''takes data from the user to create a list'''
error =True
while error == True:
error =False
user_data = raw_input("Please enter integers separated by space:" )
listofdata =user_data.split()
for item in listofdata:
try:
item = int(item)
except:
print("Please enter only numbers separated by spaces")
error =True
break
return listofdata
After the line:
listofdata =user_data.split()
you can add a check, something like:
if len(listofdata) > 10:
print "You should enter 10 integers only, please try again!"
error = True
continue
For my homework assignment, I am told to raise a key error if the key(text) the user enters contains any non alphabetic characters and reprompt. So far I have this which seems to work but obviously doesn't use the expected try/except structure
key=input("Please enter the key word you want to use: ")
ok=key.isalpha()
while (ok==False):
print("The key you entered is invalid. Please try again")
key=input("Please enter the key word you want to use")
This is not appropriate usage of KeyError (it's supposed to be used for dict lookups, or similar situations), but if it is what you have been asked to do then try something like this :
def prompt_thing():
s = raw_input("Please enter the key word you want to use: ")
if s == '' or not s.isalnum():
print("The key you entered is invalid. Please try again")
raise KeyError('non-alphanumeric character in input')
return s
s = None
while s is None:
try:
s = prompt_thing()
except KeyError:
pass