![]() |
I dont know why my function won't work? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: I dont know why my function won't work? (/thread-38823.html) |
I dont know why my function won't work? - MehHz2526 - Nov-28-2022 purchase = 1 def write(): shop =input("What shop did you shop at?") sipCard =int(input("What is your sip card?")) sipCardexipirationDate = int(input("please write the years left before the expiry date (to the nearest year) ")) cost =float(input("Please enter the total cost of the shop you have shopped at")) #monthly record with open ("MonthlyRecord.txt","w")as f: f.write ("shop:"+str(shop)) f.write (" ; ") f.write ("SIP:"+str(sipCard)) f.write (" ; ") f.write ("expiry:"+str(sipCardexipirationDate)) f.write (" ; ") f.write ("£:"+str(cost)) f.write ("\n") exit with open ("MonthlyRecord.txt","r") as f: print(f.read()) def read(): lookfor = input ("what is your name?") with open("MonthlyRecord.txt", "r") as f: for line in f.readlines(): sipCard = line.split(" ; ")[0] sipCardexipirationDate = int(line.split(" ; ")[1].replace("\n","")) if sipCard == lookfor: print(sipCardexipirationDate) choice = input("pick 1 or 2") if choice == "1": write() else: read() print ("thank you") while purchase <200: write() read()
RE: I dont know why my function won't work? - MehHz2526 - Nov-28-2022 (Nov-28-2022, 09:04 PM)MehHz2526 Wrote:but this is all of my code?purchase = 1 def write(): shop =input("What shop did you shop at?") sipCard =int(input("What is your sip card?")) sipCardexipirationDate = int(input("please write the years left before the expiry date (to the nearest year) ")) cost =float(input("Please enter the total cost of the shop you have shopped at")) #monthly record with open ("MonthlyRecord.txt","w")as f: f.write ("shop:"+str(shop)) f.write (" ; ") f.write ("SIP:"+str(sipCard)) f.write (" ; ") f.write ("expiry:"+str(sipCardexipirationDate)) f.write (" ; ") f.write ("£:"+str(cost)) f.write ("\n") exit with open ("MonthlyRecord.txt","r") as f: print(f.read()) def read(): lookfor = input ("what is your name?") with open("MonthlyRecord.txt", "r") as f: for line in f.readlines(): sipCard = line.split(" ; ")[0] sipCardexipirationDate = int(line.split(" ; ")[1].replace("\n","")) if sipCard == lookfor: print(sipCardexipirationDate) choice = input("pick 1 or 2") if choice == "1": write() else: read() print ("thank you") while purchase <200: write() read() RE: I dont know why my function won't work? - deanhystad - Nov-28-2022 The error message is very clear. 'SIP:123' is not a string that can be converted to an int. Please use Python tags when posting code. I think your program is supposed to look like this: purchase = 1 def write(): shop = input("What shop did you shop at?") sipCard = int(input("What is your sip card?")) sipCardexipirationDate = int(input("please write the years left before the expiry date (to the nearest year) ")) cost = float(input("Please enter the total cost of the shop you have shopped at")) #monthly record with open ("MonthlyRecord.txt","w")as f: f.write ("shop:"+str(shop)) f.write (" ; ") f.write ("SIP:"+str(sipCard)) f.write (" ; ") f.write ("expiry:"+str(sipCardexipirationDate)) f.write (" ; ") f.write ("£:"+str(cost)) f.write ("\n") exit with open ("MonthlyRecord.txt","r") as f: print(f.read()) def read(): lookfor = input ("what is your name?") with open("MonthlyRecord.txt", "r") as f: for line in f.readlines(): sipCard = line.split(" ; ")[0] sipCardexipirationDate = int(line.split(" ; ")[1].replace("\n","")) if sipCard == lookfor: print(sipCardexipirationDate) choice = input("pick 1 or 2") if choice == "1": write() else: read() print ("thank you") while purchase < 200: write() read()There are many problems here. Let's start with this: choice = input("pick 1 or 2") if choice == "1": write() else: read() print ("thank you")It looks like this lets the user select if the program calls write() or read(). But it is in the wrong place. It is inside the function read(). This also doesn't make much sense: while purchase < 200: write() read()Here you force the user to write() and then read(). When the user does read() they are presented with the choice to do a write() or another read(). The first thing I would do is clean up this logic. I would ignore reading and writing and just get the logic working to let the user decide what to do. def read(): """stub for read""" print("Reading") def write(): """stub for write""" print("Writing") while True: choice = input("What would you like to do (R)ead (W)rite (Q)uit: ") # what goes here?Once you have that working, you can focus on getting write() to work. In your program it doesn't work. Test with writing 1 record and then view the file. How many records are in the file? Do the records look correct? Does the file contain all the information needed to read a record in the file? Once you can write 1 record to the file, write 2 records and view the file. How many records are in the file? After writing works, test reading. I notice that reading asks for a name, but writing does not. How do you expect reading to find something that isn't written to the file? RE: I dont know why my function won't work? - deanhystad - Nov-28-2022 (Nov-28-2022, 09:20 PM)MehHz2526 Wrote: but this is all of my code?Next time follow the rules so the moderator doesn't have to fix your post. Use proper tags for the code, output and errors. Preview you post before hitting the post button to verify that everything is correct. Your first post was unreadable. |