While loop is starting even when parameters aren't met - python

I've been trying to run this Python code:
type = input("Please enter the file type ('video' or 'audio'): ")
while type != "video" or type != "audio":
type = input("Please enter a valid format ('video' or 'audio'): ")
if type == "video" or type == "audio":
break
The problem: Even when the conditions aren't met (e.g: the format is 'video') the while loop begins, you can break out of it by simply typing the required format again, but it's very inconvenient, any help?

I think the correct code is:
t = ""
while t not in ["video", "audio"]:
t = input("Please enter a valid format ('video' or 'audio'): ")

while type != "video" or type != "audio"
That while condition will ALWAYS be true.
If they entered "video", then the != "audio" part will be true.
If they entered "audio", then the != "video" part will be true.
If they entered anything else, then both parts will be true.
Use and instead of or:
while type != "video" and type != "audio"
Or, even better, check for membership in a list:
while type not in ["video", "audio"]:
And better still, don't use built-in names such as type for your variables.

Change or by and:
# HERE -------------v
while t != "video" and t != "audio":
print(t)
t = input("Please enter a valid format ('video' or 'audio'): ")

NOT x OR NOT y is not the opposite of x OR y, that's NOT (x OR y). Or, using De Morgan's laws, NOT x AND NOT y.
That said, using not in is much simpler. See Adrian B's answer.

I would put this logic in its own function to avoid complicated loop structures and make the code more readable. I would do something like this:
def get_media_type_input():
while True:
media_type = input("Please enter a valid format ('video' or 'audio'): ")
if media_type in ("video", "audio"):
return media_type
else:
print(f"Invalid media type {media_type}. Try again")
...
media_type = get_media_type_input()
You could then extend this to make the method more generic:
def get_format_type_input(title, choices):
while True:
choice = input(
f"Please enter a valid {title} format "
f"({', '.join(choices)}): "
)
if choice in choices:
return choice
else:
print(f"Invalid {title} format '{choice}'. Try again.")
media_type = get_format_type_input("media", ["video", "audio"])

Related

How to validate a string with spaces?

Currently I have this, but when entering a name with a surname, but it does not validate it accordingly
name = input("Enter your name: ")
if True == name.isalnum():
print("Correct, it's a name")
else:
print("Error")
You can try to split the input by space symbols first and then check each part. The code will be something like that:
name = input("Enter your name: ")
if all(part.isalnum for part in name.split()):
print("Correct, it's a name")
else:
print("Error")
Btw, I guess you should use isalpha instead of isalnum, because '123' not seems to be a valid name.
I think you should change your code checking for if all are digits
False == name.isdigit():

How to prevent user from inputting spaces/nothing in Python?

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.

Combine multiple while statements python

So I have for example the following while statements and I would like to combine them. Because this can get tiresome if you have 20 of these with all different if statements.
while True:
name = str(raw_input("NAME PLEASE\n"))
if name.isalpha():
break
print("Please chars dude")
while True:
age = raw_input("Please type your age\n")
if age.isdigit():
break
print("Please digits only")
If I combine them and someone types a A-Z character with 'age' then the code restarts all over without having saved the 'name' statement. I would like it to save 'name' if it's correct and only start over from the if statement that was false.
while True:
name = str(raw_input("NAME PLEASE\n"))
if name.isalpha():
break
print("Please chars dude")
age = raw_input("Please type your age\n")
if age.isdigit():
break
print("Please digits only")
Use a function to encapsulate asking for information. You can pass in a validation test function:
def ask(question, validator, errormessage):
while True:
result = raw_input(question)
if not validator(result):
print(errormessage)
continue
return result
name = ask("NAME PLEASE\n", lambda s: s.isalpha(), "Please chars dude")
age = ask("Please type your age\n", lambda s: s.isdigit(), "Please digits only")
This is far more readable then any number of tests to see if the user already entered a correct name and you only need to ask for the age now.
Why not use functions and cut down on some duplication in the process?
def ask_input(prompt, error_msg, validation_fn):
while True:
data = raw_input(prompt)
if validation_fn(data):
return data
print(error_msg)
name = ask_input("NAME PLEASE\n", "Please chars dude", lambda x: x.isalpha())
age = ask_input("Please type your age\n", "Please digits only",
lambda x: x.isdigit())
In this case, the prompt (what to ask the user), an error message (what to provide on invalid input), and a validation function are provided to the ask_input() function. This hides the while loop behind the function call and gives you something more meaningful to read in the code.
The lambda functions are just an easy way to help do the validation. You could do this instead:
def isalpha(x):
return x.isalpha()
def isdigit(x):
return x.isdigit()
name = ask_input("NAME PLEASE\n", "Please chars dude", isalpha)
age = ask_input("Please type your age\n", "Please digits only", isdigit)
You can set the variables to None first, and then check them before assignment:
name, age = None, None
while True:
if name is None:
name = str(raw_input("NAME PLEASE\n"))
if not name.isalpha():
print("Please chars dude")
name = None
continue
if age is None:
age = raw_input("Please type your age\n")
if not age.isdigit():
print("Please digits only")
age = None
continue
print("input is valid")
break
continue will start the loop over again. This fits better in the logic of your code, since break actually stop and exit the loop code.
Just use flags to track weather valid input is given, if given then exit the loop.
name_input_required = True
name = ''
while name_input_required:
name = str(raw_input("NAME PLEASE\n"))
if name.isalpha():
name_input_required = False
else:
print("Please chars dude")
age_input_required = True
age = None
while age_input_required:
age = raw_input("Please type your age\n")
if age.isdigit():
age_input_required = False
else:
print("Please digits only")
Try this:
name = None
age = None
while requires_info:
if name is None:
temp_name = str(raw_input("NAME PLEASE\n"))
if temp_name.isalpha():
name = temp_name
continue
else:
print("Please chars dude")
continue
if age is None:
temp_age = raw_input("Please type your age\n")
if temp_age.isdigit():
age = temp_age
continue
else:
print("Please digits only")
continue
break
What we do here is use a single continuous loop and a few if statements/variables to track what still needs to be done. Note depending on how you want them to enter the data you may also add logic to not ask for age if the name was invalid.
So I have for example the following while statements and I would like to combine them. Because this can get tiresome if you have 20 of these with all different if statements.
I assume the actual problem is "How to reduce tiresome code?" instead of "How to merge two loops into one?". I think keeping two loops is a good idea.
def safe_input(prompt, err_message, validation_fn):
while True:
value = raw_input(prompt)
if validation_fn(value):
return value
print err_message
name = safe_input("NAME PLEASE\n", "Please chars dude", str.isalpha)
age = safe_input("Please type your age\n", "Please digits only", str.isdigit)
If you always want the used to enter text in a separate line, you might want to print prompt before raw_input and to not give an argument to raw_input. That way you don't have to supply "\n" in every call of safe_input.
Yes, you can combine both loops in one loop!
Always try to solve the problem line by line.
You will get to learn the language better and it is the most simple way to solve any problem too.
A line by line solution would look like this:
name = '' # define name outside while loop
while True:
if not name:
name = str(raw_input("NAME PLEASE\n"))
if not name.isalpha(): # validate name
print("Please chars dude")
# reset name
name = ''
else:
age = raw_input("Please type your age\n")
if age.isdigit(): # validate age
"""continue your code here"""
print('name: ' + name + ' and age: ' + age)
print('Ok! Goodbye!')
break # while loop
else:
print("Please digits only")
will print:
NAME PLEASE
Elis
Please type your age
30
name: Elis and age: 30
Ok! Goodbye!
This will help you understand while loop better and how to use it in more difficult cases.
Do not over design using redundant language features. It will make refactoring and debugging difficult.

else statement python programming

How can I link the second info() call to a text file ?
print("Hi,welcome to the multiple choice quiz")
def info ():
print("Firstly we would like to collect some personal details:-?")
name = input("Please enter your first name?")
surname =input ("please enter your surname?")
email_address = input ("please enter your email addres #.co.uk")
username = input (" Chose a username?")
password = input ("Enter a Password?")
validation()
def validation():
correct = 0
while correct == 0:
correct=input(" is the following data is correct ?")
if correct in ["Y,y"]:
print("Well done you have registered for the quiz")
elif correct in ["N,n"]:
info()
else:
info()
You might want to drop your while loop.
info() calls your validation. "if" the data is correct, you finish, "else" you just call info() again.
That is pretty much what you did already. maybe you wanted it to look more like this:
print("Hi, welcome to the multiple choice quiz")
def info ():
print("Firstly we would like to collect some personal details:-?")
name = input("Please enter your first name?")
surname = input("please enter your surname?")
email_address = input("please enter your email address #.co.uk")
username = input(" Chose a username?")
password = input("Enter a Password?")
validation()
def validation():
correct = 0
correct = input(" is the data is correct ?")
if correct in ["Y,y"]:
print("Well done you have registered for the quiz")
elif correct in ["Y,y"]:
info()
else:
print "please type y or n"
validation()
info()
You wrote:
if correct in ["Y,y"]:
print("Well done you have registered for the quiz")
elif correct in ["N,n"]:
info()
else:
info()
Firstly, correct in ["Y,y"] will not do what you expect. ["Y,y"] is a list containing one element: "Y,y". That means correct in ["Y,y"] if and only if correct == "Y,y", and the user is not likely to enter that! You probably want a list with two elements: ["Y", "y"]. in can also test containment within strings, so you could use "Yy". You don't want a comma in there because then if the user enters just a comma that will pass the test as well, which is silly.
Putting that issue aside, if the user enters y or Y, it prints well done. If they enter n or N, info() is called. If they enter something else, info() is still called. That last part is surely not what you want: entering "N" and entering "A" should have different results! You want the user to be told that it's not valid. That part is easy, print a message. Then you want them to be asked again. Since they'll be asked for every iteration of your while loop, you just have to ensure that the loop continues. The loop will run as long as the condition correct == 0 is true, so just set correct to 0 and that will happen. So something like this:
else:
print("That's not valid")
correct = 0
OK, now to your question. You want to save those personal details to a file. You can't do that properly with the way your program is organised. The variables in info are local, so validation can't access them to save them. info has to do the saving (perhaps indirectly by passing the data along to another function). What we could do is have validation report the result of asking the user and let info decide what to do based on that:
def info ():
# collect data
if validation():
# save data
def validation():
correct = 0
while correct == 0:
correct=input(" is the following data is correct ?")
if correct in ["Y", "y"]:
print("Well done you have registered for the quiz")
return True
elif correct in ["N", "n"]:
return False
else:
print("That's not valid")
correct = 0
return will exit the validation function (thus ending the loop) and give the value returned to info to decide if the data should be saved.
Since the return statements end the loop, the only way the loop can continue is the else is reached, in which case explicitly making it continue with correct = 0 is silly. We can just make the loop go on forever until the function returns:
def validation():
while True:
correct=input(" is the following data is correct ?")
if correct in ["Y", "y"]:
print("Well done you have registered for the quiz")
return True
elif correct in ["N", "n"]:
info()
return False
else:
print("That's not valid")

I am making a function to detect input types in Python and I can't make it work

So I am making a prime number detector as a project. I’m VERY new to programming and my friend showed me a little python. I want to make a function that detects if the user puts in a number for the input (like 5,28,156,42,63) and if the put in something else (like banana,pants,or cereal) to give them a custom error saying "Invalid Number. Please Try Again" and then looping the program until they put in a number.
Please help me make this work.
def number_checker():
user_number = int(input('Please enter a Number: '))
check = isinstance(user_number, int)
if check == True:
print ('This is a number')
if check == False:
print ('This is not a number')
1) Casting input to int would raise an exception if the input string cannot be converted to int.
user_number = int(input('Please enter a Number: '))
^^^
2) It does not make sense to cross-verify user_number with int instance as it would already be int
3) You can try
def number_checker():
while not input("enter num: ").isdigit():
print("This is not a number")
print("This is a number")
number_checker()
Try following:
def number_checker():
msg = 'Put your number > '
while True:
user_input = input(msg)
correct = user_input.isdigit()
if correct:
print("This is an integer")
return # here you can put int(user_input)
else:
print("This is not an integer")
msg = 'You typed not integer. Try again.> '
if __name__ == '__main__':
number_checker()
And it's also good rule give names for your functions as verbs according to what they do. For this one, I would give, for example def int_input or something.

Categories