Как мне сохранить вновь созданные изображения с именем префикса и номером итерации

#python #loops #for-loop #iteration #prefix

#python #циклы #для цикла #итерация #префикс

Вопрос:

я создаю функцию для создания цветных квадратных плиток, зависящих от функции createSquareTile(numBiles, prefix). Как мне убедиться, что файл сохраняется под определенным префиксом, указанным при вызове функции, а также количество раз, когда он был создан. Кроме того, как мне выполнить цикл этих кодов в соответствии с количеством раз, которое упоминается в ‘numBiles’?

Например. createSquareTile(2, squaretile), он создаст 2 плитки и сохранится как squaretile1, squaretile2

numTiles = количество создаваемых плиток

префикс = имя префикса для сохраняемых фрагментов как

код:

 def createSquareTile(numTiles, prefix):
  current = 1 #iteration number
  if current == 1: #Checks if first tile created
    size = requestNumber("Please input a positive number")
    sizeI = int(size)

  while (sizeI < 0): #If invalid input 
      showError("Your input number"   str(sizeI)  "is not valid. Do it again!")
      size = requestNumber("Please input a positive number")

    if (sizeI > 0): #if input number is positive
      current = current   1
      color = pickAColor()
      newTile = makeEmptyPicture(sizeI, sizeI, color)
      setMediaPath()
      writePictureTo(newTile, getMediaPath(string(prefix)   current))

  elif current > 1: #Checks if not first tile created
    previousChoice = requestString("Same size as pervious tile? (Y/N)")

    while (previous != "N") and (previous != "Y"):
      showError("Your input character"   previousChoice   "is not valid. Do it again!")
      previousChoice = requestString("Same size as previous tile? (Y/N)")

    if (previousChoice == "Y"): #If user wants to create the next tile with the same size as the previous
      current = current   1
      colorY = pickAColor()
      newTileY = makeEmptyPicture(sizeI, sizeI, colorY)
      setMediaPath()
      writePictureTo(newTileY, getMediaPath(string(prefix)  current))


    elif (previousChoice == "N"): #If not the first title made and user does not want previous size
      sizeN = requestNumber("Please input a positive size number")
      sizeNI = int(numTilesN)
      current = current   1
      colorN = pickAColor()
      newTileN = makeEmptyPicture(sizeNI, sizeNI, colorN)
      setMediaPath()
      writePictureTo(newTileN, getMediaPath(string(prefix)  current))
  

Комментарии:

1. Не уверен, в чем проблема. Почему это не for n in range... сработает?

2. @Jacob Ах да, у меня возникли проблемы с поиском диапазона для его запуска, но я обнаружил, что for i in range(numTiles) это работает! Спасибо!