Could not convert string to float? - python

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...

Related

Validate user input with data in .txt file

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.')

How do I put each record inputted by a user within a loop in a text file

So, I have my code which is for a user wanting to loan books from a library or somewhere. They input the code for the book that they'd like to loan, I have the code with the assigned name, price etc in a text file. They input the quantity of the book that they'd like to loan then a price is calculated. If they want to loan more books, the code loops back and they can repeat the process. I want to make a separate receipt file, which stores everything that the user enters within the loop in this receipt file, and then adds everything together. I read some stuff and tried this, but there is an error and I don't know what I did wrong. Here is my code:
task=input("Enter 'b' to borrow a book, press 'x' to exit. \n")
if task.lower()== "b":
myfile=open("books.txt", "r+")
details=myfile.readlines()
while True:
book=input("Enter the 8 digit book code. \n")
while len(book) !=8
print("Your code is not 8 digits long, please try again.\n")
f = open("books.txt", "r+")
for line in f.readlines():
quantity=input("How many copies of the book do you wish to purchase?\n")
t = line.split(" ")
price = float(t[3])
code = t[1]
NameOfBook = t[2]
total=(price)* int(quantity)
with open("receipt.txt", "w") as receiptFile:
receiptFile.write(str(code))
receiptFile.write(float(NameOfItem))
receiptFile.write(int(quantity))
receiptFile.write(str(price))
receiptFile.write(str(total))
break
answer = input("Do you want to loan another book? Enter 'yes' if you would like to. ")
if answer == "yes":
continue
else:
break
What I tried to enter is this:
with open("receipt.txt", "w") as receiptFile:
receiptFile.write(str(code))
receiptFile.write(float(NameOfBook))
receiptFile.write(int(quantity))
receiptFile.write(str(price))
receiptFile.write(str(total))
I assumed that it would open the file that I made named receipt.txt and then write in each thing the user inputs, however I ended with no success. This came up instead:
Traceback (most recent call last):
File "F:\PYTHOn\New python edited for loop.py", line 19, in <module>
receiptFile.write(float(NameOfBook))
ValueError: could not convert string to float: 'Narnia,'
Please help if possible. thanks :)

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.

Sending information gathered from questionnaire to a Python document?

I'm brand new to both Python and StackOverflow, and I have a problem that has been stumping me for the past couple of hours.
I am making a peer-evaluation script for my high-school class. When you run the script, you input your classmate's name, then you rate them 1-10 on effort, accountability, and participation. These 3 values are then averaged. This average is assigned to the variable "grade". Since each classmate is going to get multiple grades, I need to have the "grade" variable export to another Python document where I can average every grade for each respective classmate.
So far, I have the script create a .txt file with the same name as the evaluated classmate, and the grade integer is stored there. Does anyone know of a way that I can export that integer to a Python file where I can append each successive grade so they can then be averaged?
Thanks
Python peer evaluation script
def script():
classmate = input('Please enter your classmate\'s name: ')
classmateString = str(classmate)
effortString = input('Please enter an integer from 1-10 signifying your classmate\'s overall effort during LLS: ')
effort = int(effortString)
accountabilityString = input('Please enter an integer from 1-10 signifying how accountable your classmate was during LLS: ')
accountability = int(accountabilityString)
participationString = input('Please enter an integer from 1-10 signifying your classmate\'s overall participation: ')
participation = int(participationString)
add = effort + accountability + participation
grade = add / 3
gradeString = str(grade)
print ('Your grade for ', classmate, 'is: ', grade)
print ('Thank you for your participation. Your input will help represent your classmate\'s grade for the LLS event.')
filename = (classmateString)+'.txt'
file = open(filename, 'a+')
file.write(gradeString)
file.close()
print ('Move on to next classmate?')
yes = set(['yes','y','Yes','Y'])
no = set(['no','n','No','n'])
choice = input().lower()
if choice in yes:
script()
elif choice in no:
sys.exit(0)
else:
sys.stdout.write("Please respond with 'yes' or 'no'")
script()
script()
put
import name_of_script_file
at the top of your Python file, assuming they are in the same folder.
Then you can access the variable like:
name_of_script_file.variable_name

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