Python Forum
I dont know why my function won't work?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I dont know why my function won't work?
#1
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()
Output:
What shop did you shop at?gucci What is your sip card?123 please write the years left before the expiry date (to the nearest year) 2 Please enter the total cost of the shop you have shopped at123.45 shop:gucci ; SIP:123 ; expiry:2 ; £:123.45 what is your name?mehri
Error:
Traceback (most recent call last): File "C:/Users/StaR/AppData/Local/Programs/Python/Python311/file3.py", line 43, in <module> read() File "C:/Users/StaR/AppData/Local/Programs/Python/Python311/file3.py", line 30, in read sipCardexipirationDate = int(line.split(" ; ")[1].replace("\n","")) ValueError: invalid literal for int() with base 10: 'SIP:123'
Yoriz write Nov-28-2022, 08:59 PM:
Please post all code, output and errors (in their entirety) correctly between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
(Nov-28-2022, 09:04 PM)MehHz2526 Wrote:
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()
Output:
What shop did you shop at?gucci What is your sip card?123 please write the years left before the expiry date (to the nearest year) 2 Please enter the total cost of the shop you have shopped at123.45 shop:gucci ; SIP:123 ; expiry:2 ; £:123.45 what is your name?mehri
Error:
Traceback (most recent call last): File "C:/Users/StaR/AppData/Local/Programs/Python/Python311/file3.py", line 43, in <module> read() File "C:/Users/StaR/AppData/Local/Programs/Python/Python311/file3.py", line 30, in read sipCardexipirationDate = int(line.split(" ; ")[1].replace("\n","")) ValueError: invalid literal for int() with base 10: 'SIP:123'
but this is all of my code?
Reply
#3
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?
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 311 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  Something the code dont work AlexPython 13 2,267 Oct-17-2022, 08:34 PM
Last Post: AlexPython
  why I dont get any output from this code William369 2 1,137 Jun-23-2022, 09:18 PM
Last Post: William369
  time function does not work tester_V 4 3,051 Oct-17-2021, 05:48 PM
Last Post: tester_V
  write new function or change the old one to work "smartter? korenron 3 1,989 Aug-09-2021, 10:36 AM
Last Post: jamesaarr
  string function doesn't work in script ClockPillow 3 2,423 Jul-13-2021, 02:47 PM
Last Post: deanhystad
  Why does unpickling only work ouside of a function? pjfarley3 5 3,456 Dec-24-2020, 08:31 AM
Last Post: pjfarley3
  len() function, numbers doesn't work with Geany Editor Penguin827 3 3,016 May-08-2020, 04:08 AM
Last Post: buran
  [split] import PIL dont work vedansh 1 2,096 Mar-29-2020, 10:00 AM
Last Post: Larz60+
  import PIL dont work rodink 14 12,932 Feb-22-2020, 08:48 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020