Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. lowercasing function doesn't work in a different function
Qt 6.12 LTS is out! See what's new in the release blog

lowercasing function doesn't work in a different function

Scheduled Pinned Locked Moved Unsolved Qt for Python
python
4 Posts 3 Posters 1.3k Views 1 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.
  • G Offline
    G Offline
    gorgenasty
    wrote on last edited by
    #1

    Please bear with me, I am new to Python.

    The first function I wrote was one to lowercase any uppercase characters in a string that passed through it, which works by itself, while ignoring any non-uppercase alphabetical ASCII characters.

    However, when I try to use it in my second function, (which SHOULD use the lowercasing function on whatever the user inputs and then sticks it in a file) I'm left with a file that contains the the string that's initially passed through without any of the lowercasing function.

    import os.path
    from os import path

    def lowercaser(text):
    text = [ord(c) for c in text]
    length = len(text)

    i = 0
    while length != i:
        if 65 <= text[i] <= 90:
            text[i] = text[i] + 32
        i += 1
    
    text = [chr(c) for c in text]
    text = "".join(text)
    

    def does_rfid_list_exist():
    if path.exists("rfidList.txt"):
    print("File found!")

    else:
        print("File was not located! Creating new file.\n")
        f = open("rfidList.txt", "a+")
        user_input = input("Please enter your name!\n")
        lowercaser(user_input)
        f.write(user_input)
        f.close()
    

    does_rfid_list_exist()

    I have no idea why they don't work together, and I've broken it down as far as I can. Any ideas?

    JonBJ 1 Reply Last reply
    0
    • G gorgenasty

      Please bear with me, I am new to Python.

      The first function I wrote was one to lowercase any uppercase characters in a string that passed through it, which works by itself, while ignoring any non-uppercase alphabetical ASCII characters.

      However, when I try to use it in my second function, (which SHOULD use the lowercasing function on whatever the user inputs and then sticks it in a file) I'm left with a file that contains the the string that's initially passed through without any of the lowercasing function.

      import os.path
      from os import path

      def lowercaser(text):
      text = [ord(c) for c in text]
      length = len(text)

      i = 0
      while length != i:
          if 65 <= text[i] <= 90:
              text[i] = text[i] + 32
          i += 1
      
      text = [chr(c) for c in text]
      text = "".join(text)
      

      def does_rfid_list_exist():
      if path.exists("rfidList.txt"):
      print("File found!")

      else:
          print("File was not located! Creating new file.\n")
          f = open("rfidList.txt", "a+")
          user_input = input("Please enter your name!\n")
          lowercaser(user_input)
          f.write(user_input)
          f.close()
      

      does_rfid_list_exist()

      I have no idea why they don't work together, and I've broken it down as far as I can. Any ideas?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @gorgenasty

      def lowercaser(text):
          text = ...
      
      def does_rfid_list_exist():
          user_input = input("Please enter your name!\n")
          lowercaser(user_input)
          f.write(user_input)
      

      When you pass parameters to methods, Python works on a copy of the variable passed in. If the method changes that variable it does not affect the variable passed in by the caller --- that just retains its original value. So, here your lowercaser() makes its change to a copy of user_input, and all its work is discarded when it returns. When you f.write(user_input) you are saving its original value, from user_input = input().

      Your def lowercaser(text) needs to finish with a return text, to return the altered text. And your call needs to be user_input = lowercaser(user_input).

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        Why not just use Python str lower function ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        JonBJ 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi and welcome to devnet,

          Why not just use Python str lower function ?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @SGaist
          I assume this is learning exercise! :) He references a learning course he is doing.

          1 Reply Last reply
          0

          • Login

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