Skip to content
  • Categories
  • Recent
  • Groups
  • Users
  • Tags
  • Popular
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (Lumen)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Categories
  3. Web Development
  4. can someone give me some tips on how to make Python programs that write data to external files?

can someone give me some tips on how to make Python programs that write data to external files?

Scheduled Pinned Locked Moved Web Development
11 Posts 3 Posters 114 Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • LinksL Offline
    LinksL Offline
    Links
    Clown 🤡 Pythons the goat Nintendo Switch Users Eeveelution Fan! Brazilian pepsi lovers meggy lovers NOCTURNAL Smash Ultimate straight allies!
    wrote on last edited by
    #2

    1. Wrong logic in is_minor()

    You wrote:

    if age > 18:
        is_minor = True
    else:
        is_minor = False
    

    This means someone over 18 is considered a minor, which is backwards.
    It should be:

    if age < 18:
        is_minor = True
    else:
        is_minor = False
    

    Or even shorter:

    return age < 18
    

    2. Missing parenthesis in file.write()

    In write_to_file():

    file.write(name + ", "
    

    You forgot the closing ) for that file.write call.


    3. Writing a boolean directly to a file

    file.write() needs a string.
    You’re trying to write:

    file.write(minor)
    

    But minor is a boolean (True or False). You must convert it to string:

    file.write(str(minor))
    

    4. You’re opening the file with r+

    r+ means read/write without truncating. If the file doesn’t exist, it will throw an error. If you just want to append, use "a" or "a+". If you want to overwrite, use "w".


    Fixed Code

    Here’s a working version:

    # Open in append mode
    file = open("database.txt", "a")
    
    name = input("Enter your name: ")
    age = int(input("Enter your age: "))
    
    def is_minor(age):
        return age < 18
    
    minor = is_minor(age)
    
    def write_to_file(name, age, file, minor):
        file.write(name + ", ")
        file.write(str(age) + ", ")
        file.write(str(minor) + "\n")
    
    write_to_file(name, age, file, minor)
    file.close()
    

    ᴺᴼᵂ ᴾᴸᴬᵞᴵᴺᴳ
    [Welcome to Paradise]
    .ılılılllıılılıllllıılılllıllı. - [Green Day]
    ─〇─────
    ↻ ◁ II ▷ ↺

    danniltrifonovD 1 Reply Last reply
    1
    • LinksL Offline
      LinksL Offline
      Links
      Clown 🤡 Pythons the goat Nintendo Switch Users Eeveelution Fan! Brazilian pepsi lovers meggy lovers NOCTURNAL Smash Ultimate straight allies!
      wrote on last edited by
      #3

      Hope it worked :)

      ᴺᴼᵂ ᴾᴸᴬᵞᴵᴺᴳ
      [Welcome to Paradise]
      .ılılılllıılılıllllıılılllıllı. - [Green Day]
      ─〇─────
      ↻ ◁ II ▷ ↺

      1 Reply Last reply
      👍
      1
      • LinksL Links

        1. Wrong logic in is_minor()

        You wrote:

        if age > 18:
            is_minor = True
        else:
            is_minor = False
        

        This means someone over 18 is considered a minor, which is backwards.
        It should be:

        if age < 18:
            is_minor = True
        else:
            is_minor = False
        

        Or even shorter:

        return age < 18
        

        2. Missing parenthesis in file.write()

        In write_to_file():

        file.write(name + ", "
        

        You forgot the closing ) for that file.write call.


        3. Writing a boolean directly to a file

        file.write() needs a string.
        You’re trying to write:

        file.write(minor)
        

        But minor is a boolean (True or False). You must convert it to string:

        file.write(str(minor))
        

        4. You’re opening the file with r+

        r+ means read/write without truncating. If the file doesn’t exist, it will throw an error. If you just want to append, use "a" or "a+". If you want to overwrite, use "w".


        Fixed Code

        Here’s a working version:

        # Open in append mode
        file = open("database.txt", "a")
        
        name = input("Enter your name: ")
        age = int(input("Enter your age: "))
        
        def is_minor(age):
            return age < 18
        
        minor = is_minor(age)
        
        def write_to_file(name, age, file, minor):
            file.write(name + ", ")
            file.write(str(age) + ", ")
            file.write(str(minor) + "\n")
        
        write_to_file(name, age, file, minor)
        file.close()
        
        danniltrifonovD Offline
        danniltrifonovD Offline
        danniltrifonov
        Coders Русский(Russian) i love cats Classical Pianists Sad Times did the dew Pythons the goat NOCTURNAL Generic BrowseDNS Chat slim shady gang THE LIT GANG Nathaniel follower resource center
        wrote on last edited by
        #4

        @Links Thank you so much! I see what your saying, so I need to fix the is_minor logic, I'm forgetting the ")" in the file.write statement, and I need to converrt the Bool from is_minor into a string. And also, thank you you telling me to open it with "a+", I was very confused with that. Thank you again for your help!

        Do not go gentle into that goodnight...

        LinksL 1 Reply Last reply
        1
        • danniltrifonovD danniltrifonov

          @Links Thank you so much! I see what your saying, so I need to fix the is_minor logic, I'm forgetting the ")" in the file.write statement, and I need to converrt the Bool from is_minor into a string. And also, thank you you telling me to open it with "a+", I was very confused with that. Thank you again for your help!

          LinksL Offline
          LinksL Offline
          Links
          Clown 🤡 Pythons the goat Nintendo Switch Users Eeveelution Fan! Brazilian pepsi lovers meggy lovers NOCTURNAL Smash Ultimate straight allies!
          wrote on last edited by
          #5

          @danniltrifonov You're welcome!

          ᴺᴼᵂ ᴾᴸᴬᵞᴵᴺᴳ
          [Welcome to Paradise]
          .ılılılllıılılıllllıılılllıllı. - [Green Day]
          ─〇─────
          ↻ ◁ II ▷ ↺

          1 Reply Last reply
          0
          • TheAndyboiT Offline
            TheAndyboiT Offline
            TheAndyboi
            Anti Cringe Level I ♥ Nirvana
            wrote on last edited by
            #6

            theres always chatgpt 🙏

            rip dort

            https://browsedns.net/groups/a-c-a-the-anti-cringe-association

            LinksL 1 Reply Last reply
            0
            • TheAndyboiT TheAndyboi

              theres always chatgpt 🙏

              LinksL Offline
              LinksL Offline
              Links
              Clown 🤡 Pythons the goat Nintendo Switch Users Eeveelution Fan! Brazilian pepsi lovers meggy lovers NOCTURNAL Smash Ultimate straight allies!
              wrote on last edited by
              #7

              @Andyboi yeah actually but he sometimes is outdated so its better to just check the forms

              ᴺᴼᵂ ᴾᴸᴬᵞᴵᴺᴳ
              [Welcome to Paradise]
              .ılılılllıılılıllllıılılllıllı. - [Green Day]
              ─〇─────
              ↻ ◁ II ▷ ↺

              1 Reply Last reply
              0
              • danniltrifonovD Offline
                danniltrifonovD Offline
                danniltrifonov
                Coders Русский(Russian) i love cats Classical Pianists Sad Times did the dew Pythons the goat NOCTURNAL Generic BrowseDNS Chat slim shady gang THE LIT GANG Nathaniel follower resource center
                wrote on last edited by
                #8

                @Links exactly, plus chatgpt doesn't even work that well on the switch

                Do not go gentle into that goodnight...

                LinksL 1 Reply Last reply
                0
                • danniltrifonovD danniltrifonov

                  @Links exactly, plus chatgpt doesn't even work that well on the switch

                  LinksL Offline
                  LinksL Offline
                  Links
                  Clown 🤡 Pythons the goat Nintendo Switch Users Eeveelution Fan! Brazilian pepsi lovers meggy lovers NOCTURNAL Smash Ultimate straight allies!
                  wrote on last edited by
                  #9

                  @danniltrifonov Yeah and chat gpt cant even write code. it can just correct code

                  ᴺᴼᵂ ᴾᴸᴬᵞᴵᴺᴳ
                  [Welcome to Paradise]
                  .ılılılllıılılıllllıılılllıllı. - [Green Day]
                  ─〇─────
                  ↻ ◁ II ▷ ↺

                  1 Reply Last reply
                  0
                  • TheAndyboiT Offline
                    TheAndyboiT Offline
                    TheAndyboi
                    Anti Cringe Level I ♥ Nirvana
                    wrote on last edited by
                    #10

                    ive heard chatgpt can write code

                    rip dort

                    https://browsedns.net/groups/a-c-a-the-anti-cringe-association

                    LinksL 1 Reply Last reply
                    0
                    • TheAndyboiT TheAndyboi

                      ive heard chatgpt can write code

                      LinksL Offline
                      LinksL Offline
                      Links
                      Clown 🤡 Pythons the goat Nintendo Switch Users Eeveelution Fan! Brazilian pepsi lovers meggy lovers NOCTURNAL Smash Ultimate straight allies!
                      wrote on last edited by Links
                      #11

                      @Andyboi it can but it always glitches and gives LOADS OF error messages. (The code)

                      ᴺᴼᵂ ᴾᴸᴬᵞᴵᴺᴳ
                      [Welcome to Paradise]
                      .ılılılllıılılıllllıılılllıllı. - [Green Day]
                      ─〇─────
                      ↻ ◁ II ▷ ↺

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Groups
                      • Users
                      • Tags
                      • Popular