Validate user input with data in .txt file - python

I have searched and searched and tried everything. I am creating a game where the user will input a pre-assigned pin and I want to validate that pin against a .txt file in Python. I have tried so many different lines of code and my result is either everything is valid or nothing is valid. What am I doing wrong?
The pins are formatted on each line and are alpha numeric like this...
1DJv3Awv5
1DGw2Eql8
3JGl1Hyt7
2FHs4Etz4
3GDn9Buf8
1CEa9Aty0
2AIt9Dxz9
5DFu0Ati4
3AJu9Byi4
1EAm0Cfn1
3BEr0Gwk0
7JAf8Csf8
4HFu0Dlf4
Here is what I have:
user_input = input('Please enter your PIN: ')
if user_input in open("PINs.txt").read():
print('Congratulations! Click the button below to get your Bingo Number.')
else:
print('The PIN you entered does not match our records. Please check your PIN and try again.')

Try using .readlines(), this way you have to match the whole string:
user_input = input('Please enter your PIN: ') + "\n" # Adding \n to conform to readlines
if user_input in open("PINs.txt").readlines():
print('Congratulations! Click the button below to get your Bingo Number.')
else:
print('The PIN you entered does not match our records. Please check your PIN and try again.')
Small refactoring:
with open("PINs.txt") as pinfile: # Make sure file is closed
user_input = input('Please enter your PIN: ')
for pin in pinfile: # Iterate line by line, avoid loading the whole file into memory.
if pin.rstrip() == user_input: # Remove newline using .rstrip()
print('Congratulations! Click the button below to get your Bingo Number.')
break
else: # Note the indentation, the 'else' is on the 'for' loop.
print('The PIN you entered does not match our records. Please check your PIN and try again.')
In fact, you can avoid using .readlines() altogether, this utilizes the fact file objects iterate over lines, and is better for memory too:
user_input = input('Please enter your PIN: ') + "\n" # Adding \n to conform to readlines
if user_input in open("PINs.txt"):
print('Congratulations! Click the button below to get your Bingo Number.')
else:
print('The PIN you entered does not match our records. Please check your PIN and try again.')

Related

Creating a user file input that keeps asking if entered a wrong file name

I had created a python program that asks a user to create a file (for simplicity this will just be a .txt file). The stipulation is that the file can only contain alphabets and an "_". No leading numbers and cannot contain special characters and it must contain an "." somehow for an extension. In this program I did use a regex to find a pattern for a valid file format. But here is my problem. The problem is the while logic. I don't know how to fix this problem on how if there is something wrong with the file name it will flag it and keep asking for the right one. Can anyone help me with the logic here? Thanks. Here is my code
import re
def file_creation(user_files):
with open(user_files,"w+") as f:
f.writelines(f'This is what is entered into the file {user_files}\n')
f.writelines('======================================' + '\n')
f.writelines(input('Please enter a sentence: ') + '\n')
ask = input('Do you want to add more lines: ').lower().strip()
while ask != 'n':
f.writelines(input('Please enter a sentence: ') + '\n')
ask = input('Do you want to add more lines: ').lower().strip()
if ask == 'n':
f.writelines(f'======================================' + '\n')
f.writelines('Thanks for playing!' + '\n')
f.close()
m = open(user_files,"r+")
print(m)
if __name__ == "__main__":
user_files = input('Please enter a valid file: ')
pattern1 = re.compile('[ # ! # $ % ^ & * ( ) < > ? / \ | { } ~ : ]')
while pattern1.search(user_files) is not None:
print(f'Filename can only contain an alphabet, numbers, and _.')
user_files = input('Please enter a valid file: ')
while re.search("^\d",user_files) is not None:
print(f'Filename only can start with an alphabet or _.')
user_files = input('Please enter a valid file: ')
while re.search("\.",user_files) is None:
print('Filename needs to have an extension')
user_files = input('Please enter a valid file: ')
print(f'{user_files} is a valid filename.')
file_creation(user_files)
Output should be:
Please enter a filename: abc
File name needs to have an extension.
Please enter a proper filename: abc.4#r
Filename can contain only Alphabets, digits and "_".
Please enter a proper filename: 23ab.exe
Filename only can start with Alphabets or '_'.
Please enter a proper filename: abc#$.txt
Filename can contain only Alphabets, digits and "_".
Please enter a proper filename: abc##abc.txt
Filename can contain only Alphabets, digits and "_".
Please enter a proper filename: Test1.txt
Please enter a sentence: This is the first line.
Do you want to add more lines? (Y/N) y
Please enter a sentence: This is the second line.
Do you want to add more lines? (Y/N) n
This is what's entered into file Test1.txt.
=============================
This is the first line.
This is the second line.
=============================
Do you want to create another file? (Y/N) y
Let's create another file.
Please enter a filename: 3ab.doc
Filename only can start with Alphabets or '_'.
Please enter a proper filename: Test.doc
Please enter a sentence: Test.doc the first line.
Do you want to add more lines? (Y/N) y
Please enter a sentence: Test.doc !##$%&*()
Do you want to add more lines? (Y/N) y
Please enter a sentence: Test.doe the third line.
Do you want to add more lines? (Y/N) n
This is what's entered into file Test.doc.
=============================
Test.doc the first line.
Test.doc !##$%&*()
Test.doe the third line.
=============================
Do you want to create another file? (Y/N) n
Thank you for playing!
Thanks for all your help.
Put the 3 conditions you have in a while loop using if statements instead to check for all 3. Use continue to go the start of the while loop and ask for new input on invalid input. break out of the loop of if all checks are passed.
import re
pattern1 = re.compile('[ # ! # $ % ^ & * ( ) < > ? / \ | { } ~ : ]')
while True:
user_files = input('Please enter a valid file: ')
if pattern1.search(user_files) is not None:
print('Filename can only contain an alphabet, numbers, and _.')
continue
if re.search("^\d",user_files) is not None:
print('Filename only can start with an alphabet or _.')
continue
if re.search("\.",user_files) is None:
print('Filename needs to have an extension')
continue
break

How to force the user to input in a particular format? [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 2 years ago.
Is there is a way to make sure that the user enters the input data as I want them to,
For example,
I wrote this code so the user can enter some birthdays and the script will choose one in random:
import random, re
print("keep in mind that you need to enter the date in this format dd/mm/yyyy")
cont_1 = input("please enter the informations of the 1st contestant : \n")
cont_2 = input("please enter the informations of the 2nd contestant : \n")
cont_3 = input("please enter the informations of the 3rd contestant : \n")
cont_4 = input("please enter the informations of the 4th contestant : \n")
cont_5 = input("please enter the informations of the 5th contestant : \n")
print("Thank you,")
win = cont_1 + " " + cont_2 + " " + cont_3 + " " + cont_4 + " " + cont_5
contDates = re.compile(r'\d\d/\d\d/\d\d\d\d')
ir = contDates.findall(win)
print(" And the Winner is: ", random.choice(ir))
I want to know if there is a way to force the user to write in the input in this format ../../... when he enters the first two digits a slash shows and the next two
There is not easy way to do this. The easiest solution is to just check that what the user input is correct before asking for the next input:
date_re = re.compile(r'\d\d/\d\d/\d\d\d\d')
def ask_date(prompt):
while True: # Ask forever till the user inputs something correct.
text = input(prompt)
if date_re.fullmatch(text): # Does the input match the regex completly (e.g. no trailing input)?
return text # Just return the text. This will break out of the loop
else:
print("Invalid date format. please use dd/mm/yyyy")
cont_1 = ask_date("please enter the informations of the 1st contestant : \n")
cont_2 = ask_date("please enter the informations of the 2nd contestant : \n")
cont_3 = ask_date("please enter the informations of the 3rd contestant : \n")
cont_4 = ask_date("please enter the informations of the 4th contestant : \n")
cont_5 = ask_date("please enter the informations of the 5th contestant : \n")
This also simplifies the selection process, since all dates are valid:
print(" And the Winner is: ", random.choice((cont_1, cont_2, cont_3, cont_4, cont_5))
You can check if it is correct date format like this without using regex.
import datetime
user_input = input()
try:
datetime.datetime.strptime(user_input,"%d/%m/%Y")
except ValueError as err:
print('Wrong date format')
If you want it custom:
i = input("date (dd/mm/yyyy):")
split = i.split("/")
for item in split:
try:
int(item)
except:
print("error")
exit()
if len(split) != 3 or len(split[0]) not in [1, 2] or len(split[1]) not in [1, 2] or len(split[2]) != 4:
print("error!")
else:
print("accepted!")
This makes sure that all of the items are numbers, and that there are 3 slashes, the first and second ones are two digits, and the last one is 4 digits. If you want to accept any correct date:

Could not convert string to float?

I am writing a code that basically asks for user input of an 8 digit number, that is then read from a text file to see if it is valid and then asks the user for quantity. It works fine up until it needs to calculate the total of the product (multiplied by quantity entered)? It produces this error:
Traceback (most recent call last):
File "C:\Users\User\Desktop\A453 Task 2.py", line 25, in <module>
price=float(itemsplit[2]) #price is
ValueError: could not convert string to float: 'magent,'
Here is my actual code:
loop=1
while loop==1:
print ("The Hardware Store")
print ("a - Place an order by barcode")
print ("x - Exit")
task=input("Please make your selection")
if task.lower()=="a":
print("The Hardware Store")
myfile=open("hardware_store.txt", "r") #this opens the text file
product_information=myfile.readlines() #reads the file and stores it
as a variable named variable 'details'
myfile.close() #closes the file
while True:
digits=input("Please enter your GTIN-8 code\n")
if len(digits) !=8: #if the digits aren't equal to 8 digits, the
input not accepted
print("Please enter a valid GTIN-8 code\n")
else:
break #if the code is the correct length, the loop ends
for line in product_information:
if digits in line:
productline=line
myfile=open("receipt.txt", "w") #opens receipt file
myfile.writelines("\n" + "+")
quantity=input("How much of the product do you wish to purchase?\n")
itemsplit=line.split(' ') #seperates into different words
price=float(itemsplit[2]) #price is
total=(price)*(quantity) #this works out the price
myfile.writelines("Your total spent on this product is: " +str("£:,.2f)".format(total)+"\n"))
if task.lower()=="x":
print("Thank you for visiting the hardware store, come again!")
break
else:
print("Sorry, please enter a valid input")
And here is the text file (named "hardware_store.txt")
16923577,Hammer,3.00,
78451698,32 lrg nails,2,
17825269,32 med nails,2.00,
58246375,32 sml nails,2.00,
21963780,Drill Bits set,7.00,
75124816,Lrg Brush,2.00,
78469518,Sml Brush,1.00,
58423790,Dust Pan,1.00,
88562247,32 lrg screws,2.00,
98557639,32 med screws,2.00,
37592271,32 sml screws,2.00,
50966394,screwdriver set,7.00,
75533458,wall bracket,0.70,
12345678, neodymium magent, 9.99
10101010, screws 12x50mm Pack 50, 2.79
I don't understand what is happening, it works up until you enter the desired quantity. Thanks in advance
You file hardware_store.txt has values separated on each lines by comma, not whitespace. You should split the line by ',', not by ' '.
You could also look at the CSV module in python to read your file.
In itemsplit list you have:
itemsplit[0] = "12345678,"
itemsplit[1] = "neodymium"
itemsplit[2] = "magent,"
itemsplit[3] = "9.99"
itemsplit[2] hasn't got any number to cast float.
You must use try ... except ... to catch the exception when no number is in your list item.
I've corrected your code:
so say we have a hardware_store.txtfile that contains:
12345678 5
task = ""
while task.lower() != "x":
print ("The Hardware Store")
print ("a - Place an order by barcode")
print ("x - Exit")
task=input("Please make your selection ")
if task.lower()=="a":
print("The Hardware Store")
myfile=open("hardware_store.txt", "r") #this opens the text file
product_information=myfile.readlines() #reads the file and stores it as a variable named variable 'details'
myfile.close() #closes the file
while True:
digits=input("Please enter your GTIN-8 code\n")
if not len(digits) == 8: #if the digits aren't equal to 8 digits, the input not accepted
print("Please enter a valid GTIN-8 code\n")
else:
break #if the code is the correct length, the loop ends
for line in product_information:
if digits in line:
#productline=line
myfile=open("receipt.txt", "w") #opens receipt file
myfile.write("\n" + "+")
quantity=input("How much of the product do you wish to purchase?\n")
quantity = int(quantity)
price = line.split(" ")[1]
price=float(price) #price is
total=(price)*(quantity) #this works out the price
myfile.write("Your total spent on this product is: £:({:.2f}){}".format(total, '\n'))
myfile.close()
else:
print("Sorry, please enter a valid input")
else:
print("Thank you for visiting the hardware store, come again!")
Running the code:
The Hardware Store
a - Place an order by barcode
x - Exit
Please make your selection a
The Hardware Store
Please enter your GTIN-8 code
12345678
How much of the product do you wish to purchase?
5
The Hardware Store
a - Place an order by barcode
x - Exit
Please make your selection x
Thank you for visiting the hardware store, come again!
receipt.txt:
\n
+Your total spent on this product is: £:(25.00)
You won't see \n in your text editor if you open receipt.txt, I've included the '\n' just to make it clear that there's a new line at the beginning, you'll just see an empty space in your editor followed by +Your reset of the text...

Python program giving unexpected results, why?

My python program is giving unexpected results within the regular expressions functions, when I enter a number plate for recognition, it tells me it's invalid, although it is valid and I don't know why?
I would be grateful if you could tell me what's wrong and give a possible solution, as this is a very important homework assignment.
#Start
#04/02/2016
bad=[]#initialise bad list
data="Y"#initialise value to prevent infinite loop
standardNumberPlateObj=""
NumPlate=""
import re#import re functions
import pickle#import pickle function
print ("Welcome to the number plate recognition program.\n\n")
choice=input("Press 1 to input number plates and save\nPress 2 to read binary number plate file: ")
if choice == "1":
while data=="Y":# while loop
NumPlate = input("Input Registration number in format (XX01 XXX) *With a space at the end!*:\n\n") #user input for numberplate
standardNumberPlateObj=re.match(r'\w\w\d\d\s\w\w\w\s', NumPlate, re.M|re.I)#validate that numberplate is valid
if standardNumberPlateObj:
print("Verification Success")
data=input(str("Would you like to continue? (Y/N):"))
else:
print("Verification Failed")
bad.append(NumPlate)#add numberplate to bad list
data=input(str("Would you like to continue? (Y/N):"))#ask user to continue
while data=="N":#saving number plates to file if user enters N
f = open("reg.dat", "wb")
pickle.dump(bad, f)
f.close()
print ("\nRestart the program to read binary file!")#ending message
break
elif choice == "2":
print ("\nBad number plates:\n\n")
f=open("reg.dat", "rb")
Registrations = pickle.load(f)
print(Registrations)
f.close()
else:
print ("Please enter a valid choice!")
print ("\n END of program!")
It's not really possible to tell without an example input and the expected and the actual result.
But judging from the expression \w\w\d\d\s\w\w\w\s and your example in the prompt (XX01 XXX), I'd say that your regular expression is expecting a space in the end, while your input doesn't provide one.

My program is printing value following the order of my .txt file and I don't want that to happen

I want to prompt the user to input specific data from a text file(keys) so my dictionary can give the value for each of them.
It works like this:
fin=open('\\python34\\lib\\toys.txt')
toys = {}
for word in fin:
x=fin.readline()
x = word.replace("\n",",").split(",")
a = x[0]
b=x[1]
toys[a]=str(b)
i = input("Please enter the code:")
if i in toys:
print(i," is the code for a= ", toys[i],)
else:
print('Try again')
if i == 'quit':
break
but it prints 'try again' if I input a random key from my list. (which is the following:
D1,Tyrannasaurous
D2,Apatasauros
D3,Velociraptor
D4,Tricerotops
D5,Pterodactyl
T1,Diesel-Electric
T2,Steam Engine
T3,Box Car
T4,Tanker Car
T5,Caboose
B1,Baseball
B2,Basketball
B3,Football
B4,Softball
B5,Tennis Ball
B6,Vollyeball
B7,Rugby Ball
B8,Cricket Ball
B9,Medicine Ball
but if I do it in order it works. How can I fix this program so I can input any key at any time and it will still print its corresponding value?
You need to read in the whole file before prompting for your search term. So you'll need two loops -- one to get the whole data in, and a second loop to search through he data.
Here's what your updated code will look like. I replaced the file input with an array so that I could run it using a web tool:
fin=['D1,Tyrannasaurous','D2,Apatasauros','D3,Velociraptor' ]
toys = {}
for word in fin:
x = word.replace("\n",",").split(",")
a = x[0]
b=x[1]
toys[a]=str(b)
while 1:
i = input("\nPlease enter the code:")
if i in toys:
print(i," is the code for a= ", toys[i],)
else:
print('\nTry again')
if i == 'quit':
break
Output here: https://repl.it/BVxh
To read the file into dictionary:
with open('toys.txt') as file:
toys = dict(line.strip().split(',') for line in file)
To print values corresponding to the input keys provided by a user from a command-line interactively until quit key is received:
for code in iter(lambda: input("Please enter the code:"), 'quit'):
if code in toys:
print(code, "is the code for toy:", toys[code])
else:
print(code, 'is not found. Try again')
It uses two-argument iter(func, sentinel).

Categories