Skip to content
  • Categories
  • Recent
  • Groups
  • Users
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • 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 (Zephyr)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Web Development
  3. 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 687 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.
  • Links
    Links
    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 ▷ ↺

    szczęśliwy 1 Reply Last reply
    1
    • Links
      Links
      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
      • Links 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()
        
        szczęśliwy
        szczęśliwy
        szczęśliwy
        Coders Русский(Russian) Classical Pianists Pythons the goat Learning Polish
        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...

        Links 1 Reply Last reply
        1
        • szczęśliwy szczęśliwy

          @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!

          Links
          Links
          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
          • Andyboi
            Andyboi
            Andyboi
            canida 🥀 😭🙏 rip andy Bcv army Dead Char 🌹 Anti Cringe 🟨🟥🟪🟦NASCAR
            wrote on last edited by
            #6

            theres always chatgpt 🙏

            Links 1 Reply Last reply
            0
            • Andyboi Andyboi

              theres always chatgpt 🙏

              Links
              Links
              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
              • szczęśliwy
                szczęśliwy
                szczęśliwy
                Coders Русский(Russian) Classical Pianists Pythons the goat Learning Polish
                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...

                Links 1 Reply Last reply
                0
                • szczęśliwy szczęśliwy

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

                  Links
                  Links
                  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
                  • Andyboi
                    Andyboi
                    Andyboi
                    canida 🥀 😭🙏 rip andy Bcv army Dead Char 🌹 Anti Cringe 🟨🟥🟪🟦NASCAR
                    wrote on last edited by
                    #10

                    ive heard chatgpt can write code

                    Links 1 Reply Last reply
                    0
                    • Andyboi Andyboi

                      ive heard chatgpt can write code

                      Links
                      Links
                      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

                      Hello! It looks like you're interested in this conversation, but you don't have an account yet.

                      Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

                      With your input, this post could be even better 💗

                      Register Login
                      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