#writing to text file

30 messages · Page 1 of 1 (latest)

frank ice
#

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}"
        if amount == '100':
            for screw in screwlist:
                current_stock = int(screw[4])
                current_stock -= 1
                screw[4] = f"{current_stock}"
        if amount == '200':
            for screw in screwlist:
                current_stock = int(screw[5])
                current_stock -= 1
                screw[5] = f"{current_stock}"

with open("screw.txt", "w") as f:
    for screw in screwlist:
        ",".join([screw])
        f.write(screwlist)
        print(screw)

stock()

#

i am getting the error message TypeError: sequence item 0: expected str instance, list found

brisk steeple
#

Try

",".join(screw)
f.write(screw + "\n")
frank ice
#

another error TypeError: can only concatenate list (not "str") to list

brisk steeple
#

Which line?

frank ice
#

line 101 which is f.write(screw + '\n')

brisk steeple
#

Oh 😄 Sorry

#
screw = ",".join(screw)
f.write(screw + "\n")
frank ice
#

could you explain why you need the screw = beforehand? sorry for the bother

brisk steeple
#

Same reason if you do 1 + 1 you need to store the result somewhere like x = 1 + 1

frank ice
#

oh right yes makes sense

#

its working but its doing -1 to all screw[3] in the text file

#

instead of just the 1

#

is it because of the for screw in screwlist: loop?

brisk steeple
#

It shouldn't be, that should just be modifying a single screw Can you copy and paste what you are inputting when you run it?

#

Or screenshot the terminal output with your inputs?

frank ice
#

ok

#

this is the text file before

brisk steeple
#

Oh nevermind

frank ice
#

oh

brisk steeple
#
            if amount == '50':
                for screw in screwlist:
                    current_stock = int(screw[3])
                    current_stock -= 1
                    screw[3] = f"{current_stock}"
            if amount == '100':
                for screw in screwlist:
                    current_stock = int(screw[4])
                    current_stock -= 1
                    screw[4] = f"{current_stock}"
            if amount == '200':
                for screw in screwlist:
                    current_stock = int(screw[5])
                    current_stock -= 1
                    screw[5] = f"{current_stock}"

Don't have loops there

#

The outer loop is handling which screw it is, so yeah there you are modifying all the screws

frank ice
#

oh right yes thank you

brisk steeple
#
    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':
                current_stock = int(screw[3])
                current_stock -= 1
                screw[3] = f"{current_stock}"
            if amount == '100':
                current_stock = int(screw[4])
                current_stock -= 1
                screw[4] = f"{current_stock}"
            if amount == '200':
                current_stock = int(screw[5])
                current_stock -= 1
                screw[5] = f"{current_stock}"
#

Something like that

frank ice
#

yes it works thank you

brisk steeple
#

Nice 🙂

frank ice
#

❤️

frank ice
#

see if trying to make a menu option how would i go about calling the actual function?

#

def list():
operation = input('''
Select operation:
[1] View Stock
[2] Purchase
[3] Add Stock

''')

if operation == '1':
    print("View Stock: ")
    showtable()

elif operation == '2':
    print("Purchase: ")
    purchase()

elif operation == '3':
    print("Add Stock: ")
    addstock()

else:
    print('You have not chosen a valid operator, please run the program again.')