나는 기본적으로 8 자리 숫자의 사용자 입력을 요구하는 코드를 작성하고,이 코드는 텍스트 파일에서 읽혀 유효한지 확인한 다음 사용자에게 수량을 묻습니다. 그것은 제품의 합계 (입력 된 수량 곱한) 계산해야 할 때까지 잘 작동합니까? 텍스트 파일 (명 "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
그렇게하지 여기
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")
그리고있다 :
여기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,'
내 실제 코드이다 : 그것은이 오류가 발생합니다 무슨 일이 일어나고 있는지 이해하고, 원하는 양을 입력 할 때까지 작동합니다. 미리 감사드립니다.
구분 기호는'split()'메소드에 대한'(공백)'이 아니라', (쉼표)'입니다. 공간 대신 쉼표가 있어야합니다. –