Python Forum
Python 3.8 Nested varible not updating
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3.8 Nested varible not updating
#2
Quote:I assumed if the nested variable updates the other would automatically.

No, that's not how python str()s work. They are just a sequence of characters. It's not a dynamic formula that updates. I wouldn't even call this a "nested" variable. This is just a static string that is created from the values of other strings. If the other strings change in the future, it doesn't affect the one you built.

name = "Sally"
mystring = "Hello, " + name
name = "Miguel" # Does not affect the mystring value
print(mystring) # prints "Hello Sally"

Something like this might work. I'm not sure what logic you were using for determining the number of digits that could be used. This one just hardcodes it to exactly 3. But making that dynamic on some input or other signal would not be difficult.

You can't create a directory with a conflicting name. As long as you're talking a small number of directories (1000 is still small), then it makes sense to just try to create the next one and handle the failure if it exists.

import os
from pathlib import Path

drawing_dir = Path("C:", "Users", "user", "Documents", "Client Drawings")
letters = input("Enter the first 3 letters: ")
letters = letters.upper()
for digits in range(1000):
    dir_name = drawing_dir / f"{letters}{digits:03}"
    try:
        os.mkdir(dir_name)
        print (f"Created {dir_name} as next free directory")
        break
    except FileExistsError:
        continue
else:
    print(f"All three-digit numbers taken for {letters}")
Reply


Messages In This Thread
RE: Python 3.8 Nested varible not updating - by bowlofred - Aug-27-2021, 08:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python openyxl not updating Excel file MrBean12 1 387 Mar-03-2024, 12:16 AM
Last Post: MrBean12
  local varible referenced before assignment SC4R 6 1,574 Jan-10-2023, 10:58 PM
Last Post: snippsat
  Updating nested dict list keys tbaror 2 1,311 Feb-09-2022, 09:37 AM
Last Post: tbaror
  Looping through nested elements and updating the original list Alex_James 3 2,177 Aug-19-2021, 12:05 PM
Last Post: Alex_James
  UPDATING PYTHON TO USE LATEST VERSION OF JAVA? nicklesprout 6 7,512 Jul-27-2017, 08:58 PM
Last Post: nicklesprout
  Inputting a varible BlathaBlather 3 3,623 Apr-05-2017, 08:09 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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