Jump to content

I am currently in my software class and I'm having problem again, below what the program needs to do.

 

A school in Australia needs to create a program that contains an inventory of school beanies i.e. Senior and Junior. The records need to be saved in a text file. Each week the office staff will need to open the program and enter the number of each type of beanie sold. This number is then deducted from the total. Once the number of beanies drops below 10 a warning message is provided to order more. If they select yes they can enter how many new beanies are ordered and the inventory number gets updated.

 

This is what I have so far and its not working well, the text file isn't being updated.

 

print("Junior or senior?") #Ask for year type 
answer = input()

if answer == "junior" :     #Activate the right if statement
    juniorBe = True
elif answer == "senior" :
    seniorBe = True
else :
    print("Invaild")

#If statement for Junior text file
if juniorBe == True :
    with open('junior.txt') as r:   #Open the file to read the amount from file
        amountFromfile = r.readlines()

        print("There are ", amountFromfile, " left.") #Read the amount

        r.close()
    
    print("How many do you want to sell?") #Ask for an amount to subtract

    sellAmount = input()

    with open('junior.txt', 'r') as a: #Open it again
        replaceText = a.read
        replaceText = replaceText.replace('100', 100 - sellAmount)

        """
        Replace 100 by subtracting the sell amount from 100
        (As seen above)
        """
        
        a.close()

    print("Thank you") #Prints thank you and closes
    
    exit()

 

Link to comment
https://linustechtips.com/topic/1470739-help-with-some-python/
Share on other sites

Link to post
Share on other sites

As mention above, you are reading instead of writing.

Your code is quite verbose, you can simplify it by opening the file once and using the user input to open the .txt files rather than using an if statement.

answer = input('Junior or Senior? ').lower()

# Check if user input is valid
while answer != 'junior' and answer != 'senior':
  print('Invalid input')  # Alert user that their input is invalid
  answer = input('Junior or Senior? ').lower()

filename = answer + '.txt'
file = open(filename, 'r+')  # Open file with read and write permission

beanies = int(file.readline())

print('There are currently', beanies, answer, 'beanies left.')

beanies -= int(input('How many beanies have sold: '))  # Subtract number of beanies by given amount

if beanies < 10:
  print('Notice: less than 10', answer, 'beanies are left.')

# Save us from having to open the file again. Put cursor to start of file,
# write beanie count and remove the rest of the data.
file.seek(0)
file.write(str(beanies))
file.truncate()
file.close()

print('Thank you.')

 

Link to comment
https://linustechtips.com/topic/1470739-help-with-some-python/#findComment-15679163
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×