Could anyone help me with this?
This is the text file im working with
#Listing showing screw stock details
#MATERIAL, HEAD TYPE, LENGTH, STOCK(IN BOXES OF 50,100,200), COST PER BOX of 50 (£), DISCOUNT
brass,slot,20,35,20,10,4.00, no
brass,slot,40,40,42,40,5.00, no
brass,slot,60,55,50,30,5.50, yes
brass,star,20,30,20,10,4.00, no
brass,star,40,42,33,30,5.00, no
brass,star,60,15,20,25,5.50, no
brass,pozidriv,20,20,20,10,4.00, no
brass,pozidriv,40,8,30,30,5.00, no
brass,pozidriv,60,1,20,25,5.50, no
steel,slot,20,30,30,10,3.00, no
steel,slot,40,40,30,34,4.00, no
steel,slot,60,70,50,0,4.50, no
steel,star,20,100,47,0,3.00, no
steel,star,40,19,70,5,4.00, no
steel,star,60,90,20,20,4.50, no
steel,pozidriv,20,10,40,30,3.00, no
steel,pozidriv,40,12,30,5,4.00, no
steel,pozidriv,60,25,27,20,4.50, no
#How to modify text file to 'reduce stock'
134 messages · Page 1 of 1 (latest)
Read it all into python, modify it inside python then write it all back to the file. It's not really feasible to just edit parts of files
with open("file.txt") as f:
data = f.readlines()
data[5] = "foo" # Change line 6 to say 'foo'
with open("file.txt", "w") as f:
f.writelines(data)
Roughly like that
this is the code i have so far so will i enter this into the loop?
def stock():
print('')
length = input('Enter length of the screw (20, 40, 60): ')
while length not in {'20', '40', '60'}:
length = input('Please enter a valid length: ')
material = input('Enter length of the screw (brass, steel): ')
while material not in {'brass', 'steel'}:
material = input('Please enter a valid material: ')
headtype = input('Enter head type of the screw (slot, star, pozidriv): ')
while headtype not in {'slot', 'star', 'pozidriv'}:
headtype = input('Please enter a valid head type: ')
for screw in screwlist:
if screw[0].startswith(material) and screw[1].startswith(headtype) and screw[2].startswith(length):
print(screw)
amount = input('Enter how many you would like to buy (50, 100, 200): ')
while amount not in {'50', '100', '200'}:
amount = input('Please enter a valid amount: ')
stock()
i only have to modify a certain string from a row
#Listing showing screw stock details
#MATERIAL, HEAD TYPE, LENGTH, STOCK(IN BOXES OF 50,100,200), COST PER BOX of 50 (£), DISCOUNT
brass,slot,20,35,20,10,4.00, no
brass,slot,40,40,42,40,5.00, no
brass,slot,60,55,50,30,5.50, yes
brass,star,20,30,20,10,4.00, no
brass,star,40,42,33,30,5.00, no
brass,star,60,15,20,25,5.50, no
brass,pozidriv,20,20,20,10,4.00, no
brass,pozidriv,40,8,30,30,5.00, no
brass,pozidriv,60,1,20,25,5.50, no
steel,slot,20,30,30,10,3.00, no
steel,slot,40,40,30,34,4.00, no
steel,slot,60,70,50,0,4.50, no
steel,star,20,100,47,0,3.00, no
steel,star,40,19,70,5,4.00, no
steel,star,60,90,20,20,4.50, no
steel,pozidriv,20,10,40,30,3.00, no
steel,pozidriv,40,12,30,5,4.00, no
steel,pozidriv,60,25,27,20,4.50, no
how would i go about taking away 1 from the stock of boxes
You'd just modify the string that represents the line
So if you've split on "," it might be something like
data[5][0] = "steel" # change line 6 material to steel
ah yes ok
Since it'll then be a nested list you might need to use a loop so you can join the inner list. writelines won't do that
but say if the stock is 40 how would i reduce it by 1 so that it would apply to any screw the user chooses
current_stock = int(data[5][3])
current_stock -= 1
data[5][3] = f"{current_stock}"
I'd probably do somethingl ike that
It's just representing a 2d list
I think yours would just be
for screw in screwlist:
current_stock = int(screw[3])
current_stock += 1
screw[3] = f"{current_stock}"
so [3] would represent boxes of 50?
Yeah
ok i think i understand thank you
if amount = '50'
for screw in screwlist:
current_stock = int(screw[3])
current_stock += 1
screw[3] = f"{current_stock"}
could i do something like this and change it depending on the size of the box?
def stock():
print('')
length = input('Enter length of the screw (20, 40, 60): ')
while length not in {'20', '40', '60'}:
length = input('Please enter a valid length: ')
material = input('Enter length of the screw (brass, steel): ')
while material not in {'brass', 'steel'}:
material = input('Please enter a valid material: ')
headtype = input('Enter head type of the screw (slot, star, pozidriv): ')
while headtype not in {'slot', 'star', 'pozidriv'}:
headtype = input('Please enter a valid head type: ')
for screw in screwlist:
if screw[0].startswith(material) and screw[1].startswith(headtype) and screw[2].startswith(length):
print(screw)
amount = input('Enter how many you would like to buy (50, 100, 200): ')
while amount not in {'50', '100', '200'}:
amount = input('Please enter a valid amount: ')
if amount == 50:
for screw in screwlist:
current_stock = int(screw[3])
current_stock -= 1
screw[3] = {'current_stock'}
stock()
this is the code i have for this
if amount == "50" probably
its giving me an error on the last line and also shoudl that + be a -?
Yeah sorry it should probably be a minus. What's the error?
Can you show what you have?
if amount == 50:
for screw in screwlist:
current_stock = int(screw[3])
current_stock += 1
screw[3] = f"{current_stock"}
no problem
f-strings are a relatively new and simple way to format strings in python.
Available in python 3.6 and newer.
You can read more here
thank u
no errors now but the stock doesnt change?
for screw in screwlist:
if screw[0].startswith(material) and screw[1].startswith(headtype) and screw[2].startswith(length):
print(screw)
amount = input('Enter how many you would like to buy (50, 100, 200): ')
while amount not in {'50', '100', '200'}:
amount = input('Please enter a valid amount: ')
if amount == '50':
for screw in screwlist:
current_stock = int(screw[3])
current_stock += 1
screw[3] = f"{current_stock}"
In the file? You need to write to the file
so how would i go about that i dont really understand writing to files
Similar to reading but you open in write mode and use write
Something like this
Is this homework? Do you have any notes from class?
it is but the only thing i have to go off is like videos they recorded 10 years ago so not really useful
dont explain it well
do i use both parts of teh code or just one of them
The last part that writes
screwlist probably
||(it's not going to do what you want but do it anyway so you can see :D)||
unsupported operand type(s) for -: 'list' and 'int'
What's giving that error?
line 83
What's the -1?
to reduce the stock?
You did that here?
if amount == '50':
for screw in screwlist:
current_stock = int(screw[3])
current_stock += 1
screw[3] = f"{current_stock}"
no clue what im doing haha
Try adding some prints, it's important you can see and understand what's going on
yea do i put that into the bracket of f.writelines?
Randomly writing code isn't good
No. You put screwlist. You want to write the entire contents of it to the file
so i do this first and then write it?
Yeah, that modifies screwlist then you want to write the contents of screwlist to the file
ahhh yes i see
print('The total number of units in stock in the 20 length category is: ', total1)
UnboundLocalError: local variable 'total1' referenced before assignment
getting this when i run the code?
previous part of the code
How do you think you might fix that?
It means that you're trying to print the value of total1 without defining total1 anywhere
You'd need to share the code so I can see
def showtable():
print('MATERIAL, HEAD TYPE, LENGTH, STOCK(IN BOXES OF 50,100,200), COST PER BOX of 50 (£), DISCOUNT')
for screw in screwlist:
totalstock = (int(screw[3])*50)+(int(screw[4])*100)+(int(screw[5])*200)
totalvalue = (totalstock/50)*(float(screw[6])) # <------- total stock divided by 50 multiplied by the cost of a box of 50?
print(screw)
print('The total number of units in stock is:', totalstock, 'units')
print('The total value of the stock is: £', totalvalue, '\n')
for screw in screwlist:
if screw[2].startswith('20'):
total1=(int(screw[3])*50)+(int(screw[4])*100)+(int(screw[5])*200)
if screw[2].startswith('40'):
total2=(int(screw[3])*50)+(int(screw[4])*100)+(int(screw[5])*200)
if screw[2].startswith('60'):
total3=(int(screw[3])*50)+(int(screw[4])*100)+(int(screw[5])*200)
print('The total number of units in stock in the 20 length category is: ', total1)
print('The total number of units in stock in the 40 length category is: ', total2)
print('The total number of units in stock in the 60 length category is: ', total3, '\n')
showtable()
It's because it's inside an if. If the if isn't true then total1 never gets defined
Just do this before the loop
total1 = 0
total2 = 0
total3 = 0
oh yes ok
Have you looked at what got written to the file?
theres nothing there
i have backups but not sure why its doing that
it doesnt display the table because its empty i get that bit
but nothing in text file?
Share your code
def stock():
print('Purchase Screws')
length = input('Enter length of the screw (20, 40, 60): ')
while length not in {'20', '40', '60'}:
length = input('Please enter a valid length: ')
material = input('Enter length of the screw (brass, steel): ')
while material not in {'brass', 'steel'}:
material = input('Please enter a valid material: ')
headtype = input('Enter head type of the screw (slot, star, pozidriv): ')
while headtype not in {'slot', 'star', 'pozidriv'}:
headtype = input('Please enter a valid head type: ')
for screw in screwlist:
if screw[0].startswith(material) and screw[1].startswith(headtype) and screw[2].startswith(length):
print(screw)
amount = input('Enter how many you would like to buy (50, 100, 200): ')
while amount not in {'50', '100', '200'}:
amount = input('Please enter a valid amount: ')
if amount == '50':
for screw in screwlist:
current_stock = int(screw[3])
current_stock -= 1
screw[3] = f"{current_stock}"
with open("screw.txt", "w") as f:
f.writelines(screwlist)
print(screw)
stock()
Don't do it inside the loop
And you'll need to put the original contents of the file back again before you run your program
so like this?
if amount == '50':
for screw in screwlist:
current_stock = int(screw[3])
current_stock -= 1
screw[3] = f"{current_stock}"
with open("screw.txt", "w") as f:
f.writelines(screwlist)
print(screw)
doesnt show on thsi but its in line with if amount
def stock():
print('Purchase Screws')
length = input('Enter length of the screw (20, 40, 60): ')
while length not in {'20', '40', '60'}:
length = input('Please enter a valid length: ')
material = input('Enter length of the screw (brass, steel): ')
while material not in {'brass', 'steel'}:
material = input('Please enter a valid material: ')
headtype = input('Enter head type of the screw (slot, star, pozidriv): ')
while headtype not in {'slot', 'star', 'pozidriv'}:
headtype = input('Please enter a valid head type: ')
for screw in screwlist:
if screw[0].startswith(material) and screw[1].startswith(headtype) and screw[2].startswith(length):
print(screw)
amount = input('Enter how many you would like to buy (50, 100, 200): ')
while amount not in {'50', '100', '200'}:
amount = input('Please enter a valid amount: ')
if amount == '50':
for screw in screwlist:
current_stock = int(screw[3])
current_stock -= 1
screw[3] = f"{current_stock}"
with open("screw.txt", "w") as f:
f.writelines(screwlist)
stock()
Like that
Right at the end of everything
ah yes ok
TypeError: write() argument must be str, not list for f.writelines(screwlist)
Might be because of the inner lists, try just write I'm not sure how writelines is implemented internally
Ultimately I think you'll need to do something like this anyway
TypeError: write() argument must be str, not list
Yeah okay you'll need a loop
so for screw iun screwlist: and then with open...?
Open then loop
You'll need to join the inner loop (screw) to make it a string then write that
how do i join it?
Have you not come across join before?
nope
Okay then use another loop
so what i had earlier wouldnt work?
Try it
You need to make screw (a list) into a string so you can write it to the file
so how do i do that
How do you think you'd do that?
after for loop do str(screw)?
Try it
I'm concerned I'm just telling you what to do and you don't understand what's happening or why you are getting these errors. If you're asked to explain the homework or you've done something you've not been taught it'll look suspicious
I'm happy to try to help but you need to try some stuff on your own
same error they expect us to know what this all means because they uploaded the videos but no one understands anything but they dont really seem to care i try watching youtube videos to explain it better and some online notes but most of it isnt what were trying to do in class
im happy enough if you explained it and i tried it but its just so frustrating that they dont really teach us anything
like we havent been taught the join function as it wasnt shown in the videos so they just said we dont need it yet
!eval ",".join(["foo", "bar", "baz"])
>>> ",".join(["foo", "bar", "baz"])
'foo,bar,baz'