Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 String being broken up into single characters
#1
My program is here

When I run the program, I enter in a few colors, usually blue, green, yellow typed like that. When it gets to the end it prints out "Random color for band 1 is e" instead of something like "Random color for band 1 is Green" I created a new program here using just a little bit of the code from the two sections below and it seems to work fine but I can't find what is making it print just one letter instead of the whole word.

Lines 76-99
def askcolornames():
  explaincolors()
  print("First you will pick the colors you want to include in your blanket, max of 5 separated by commas.")

  Color_Names = input("Enter your color names\n\n")
  xxx = Color_Names.split(',')
  for i in range(len(xxx)):
    xxx[i] = xxx[i].rstrip()
    xxx[i] = xxx[i].lstrip()
    xxx[i] = xxx[i].capitalize()
  # delete the null elements (requires for or while loop)
  j = 0
  while len(xxx)>j:
    if []==xxx[j]:
      del xxx[j]
      continue
    j += 1
  if debug[0]:
    print("debug 0:", Color_Names, xxx)
  xxx.sort()
  Color_Names = xxx
  random.shuffle(Color_Names)
  return Color_Names[0]
Lines 122-145
def createpattern():
  ###Put your stuff here
  Camp = []
  currentband = 1
  for i in range(Total_Rows):
    if 0==i:
      #marking first row
      print(random.choice(Color_Names))
      currentcolor = (random.choice(Color_Names))
      if debug:
        print ("Random Color for Band",currentband," is ", currentcolor)
      currentbandstart = 0
      currentbandwidth = MinRows + random.randrange(MaxRowWidth)
      #set currentbandwidth so that the width never goes past
      #the end of the pattern and the following line is unnecessary
      currentbandend = min(MaxRows, currentbandstart + currentbandwidth)
      # first pick lastcolor = a color from all choices
      # mark bandstart = 0
      # mark bandend = min_rows + random integer from 0 to (maxrowwidth - 1)


      #rowtotal = Color_Names[random.randint(0, len(Color_Names) - 1)]
    
  return None
Quote
#2
I think the problem is that the contents of the Color_Names variable is inconsistent at different parts of the file. If Color_Names' value is the list ['Red', 'Green', 'Blue', 'Yellow'], then calling random.choice(Color_Names) returns one of these colors, such as 'Blue'. But at the end of askcolornames(), you return Color_Names[0], which would be 'Red' in our case. If Color_Names is the single word 'Red', the call random.choice(Color_Names) will return a random letter from the word 'Red'. This random letter could be 'e'. So you need to be consistent in the use of this variable, make sure that Color_Names is always a list of strings and not a single string.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Python DateTime is broken 10OctNotOct10a1 8 413 Jan-03-2020, 07:54 AM
Last Post: snippsat
  Replacing characters in a string with a list cjms981 1 169 Dec-30-2019, 10:50 PM
Last Post: micseydel
  Broken interpreter? fcktheworld587 1 109 Dec-26-2019, 08:29 PM
Last Post: snippsat
  Python convert multi line into single line formatted string karthidec 2 215 Dec-23-2019, 12:46 PM
Last Post: karthidec
  Select single letters from string based on separate string Jpen10 3 467 Dec-15-2019, 01:21 PM
Last Post: Jpen10
  Strange Characters in JSON returned string fioranosnake 4 233 Dec-02-2019, 07:25 PM
Last Post: fioranosnake
  Split a long string into other strings with no delimiters/characters krewlaz 4 194 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  Print string in a single line RavCOder 8 274 Nov-08-2019, 09:45 AM
Last Post: perfringo
  How to iterate over some characters in a string and the others will stay as it is. ? sodmzs 9 588 Jun-17-2019, 06:45 PM
Last Post: perfringo
  modify the color a string of characters in python 3.6 atlass218 10 1,033 Feb-28-2019, 03:20 PM
Last Post: atlass218

Forum Jump:


Users browsing this thread: 1 Guest(s)