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


Messages In This Thread
I dont know why my function won't work? - by MehHz2526 - Nov-28-2022, 09:04 PM

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