#having trouble with strip in python

36 messages · Page 1 of 1 (latest)

light haven
#

okay so im making a dictionary map where it uses different symbols to refrence trees and mountains ect.
Anyways in Line 19 is where im trying to strip part of the string
the symbols are showing up as [' ▲'] instead of just ▲

import random

def createMap(size):
    m = {}
    m["grid"] = []                      
    m["size"] = size                    #map size (square) 

    #Make the empty grid
    for x in range(m["size"]):
        m["grid"].append([])           #make it a size-by-size 2D list

    #fill it with the fillCharacter
    for row in range(m["size"]):                           
        for x in range(m["size"]):
            symbolList = ["  ≈", "  ▲", "  ♣", "  ░", "  ▒"]
            weights = fillMap(row,x,m)
            weightedBiome = random.choices(symbolList, weights, k=1)
            weightedBiome[0].strip(']')
            try:
                m["grid"][row].append(weightedBiome)
            except IndexError:
                continue
    
    return m

def fillMap(x,y,m):
    weights = (1,1,1,1,1)
    try:
        if m["grid"][y][x-1] == "  ≈":
            weights = (4, 2, 2, 0, 2)
        elif m["grid"][y][x-1] == "  ▲":
            weights = (2, 3, 2, 2, 1)
        elif m["grid"][y][x-1] == "  ♣":
            weights = (2, 2, 4, 0, 2)
        elif m["grid"][y][x-1] == "  ░":
            weights = (0, 2, 0, 5, 2)
        elif m["grid"][y][x-1] == "  ▒":
            weights = (2, 1, 2, 3, 2)
        if m["grid"][y-1][x-1] == "  ≈":
            weights += (4, 2, 2, 0, 2)
        elif m["grid"][y-1][x-1] == "  ▲": 
            weights += (2, 3, 2, 2, 1)
        elif m["grid"][y-1][x-1] == "  ♣":
            weights += (2, 2, 4, 0, 2)
        elif m["grid"][y-1][x-1] == "  ░":
            weights += (0, 2, 0, 5, 2)
        elif m["grid"][y-1][x-1] == "  ▒":
            weights += (2, 1, 2, 3, 2)
        if m["grid"][y-1] == "  ≈":
            weights += (4, 2, 2, 0, 2)
        elif m["grid"][y-1] == "  ▲":
            weights += (2, 3, 2, 2, 1)
        elif m["grid"][y-1] == "  ♣":
            weights += (2, 2, 4, 0, 2)
        elif m["grid"][y-1] == "  ░":
            weights += (0, 2, 0, 5, 2)
        elif m["grid"][y-1] == "  ▒":
            weights += (2, 1, 2, 3, 2)
        if x < m["size"]:
            if m["grid"][y-1][x+1] == "  ≈":
                weights += (4, 2, 2, 0, 2)
            elif m["grid"][y-1][x+1]== "  ▲":
                weights += (2, 3, 2, 2, 1)
            elif m["grid"][y-1][x+1] == "  ♣":
                weights += (2, 2, 4, 0, 2)
            elif m["grid"][y-1][x+1] == "  ░":
                weights += (0, 2, 0, 5, 2)
            elif m["grid"][y-1][x+1] == "  ▒":
                weights += (2, 1, 2, 3, 2)
        #  if m["grid"][y][x-1] or m["grid"][y-1][x-1] or m["grid"][y-1] or m["grid"][y-1][x+1] == "▒":
            #weights = weights(1,1,1,1,0)
    except IndexError:
        pass
    return weights
    
    

def displayMap(m):
    #Top label
    listNum = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    count = 1
    solution = (f" │{count}")
    print("0 ", end="")
    for x in range(m["size"]):
        if count in listNum:
            solution = (f" │{count}")
        elif count == 0:
            solutin = (f" {count} ")
        else:
            solution = (f"│{count}")
        count += 1
        print(solution, end="")
    print()

    #Each row
    count = 1
    listNum = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    solution = count
    for row in m["grid"]:
        if count in listNum:
            solution = (f"{count} ")
        else:
            solution = count
        print(solution, end="")
        for col in row:
            print(col, end="")
        count += 1
        print()

#Add something to the map
def add(m, row, col, symbol):
    m["grid"][row][col] = symbol

def main():
    mapSize = 30
    halfMap = round(mapSize / 2)
    room1 = createMap(mapSize)
    add(room1, halfMap, halfMap, "  ⌂")
    displayMap(room1)
main()
#

also Is there a simple way to multiply weights with other weights?
or should i just make the weights into variables and multiply them?

stray vortex
#

You'd have to add the strip to be a variable, and use that variable instead

#

It won't do anything if you just use it by writing it into the code

#

!exec

txt = ".....Hello....."
txt.strip(".")
print(txt)

x = txt.strip(".")
print(x)
warm cedarBOT
light haven
#

i tried thius but it changes nothing

    for row in range(m["size"]):                           
        for x in range(m["size"]):
            symbolList = ["  ≈", "  ▲", "  ♣", "  ░", "  ▒"]
            weights = fillMap(row,x,m)
            weightedBiome = random.choices(symbolList, weights, k=1)
            strippedBiome = weightedBiome[0].strip('[]')
            try:
                m["grid"][row].append(weightedBiome)
            except IndexError:
                continue
#

it works but nothing is stripped

stray vortex
#

Probably want to change the variable you append

light haven
#

wops yea i did that i got
ValueError: The number of weights does not match the population

#

that doesnt make sense

#

im not even changing anything with the weights

stray vortex
#

What's the full trace and error?

light haven
#

oh sorry here

#

Traceback (most recent call last):
File "C:\Users\Erico\OneDrive\Desktop\LASTPROJECTMAP\fullrandomMappython.py", line 123, in <module>
main()
File "C:\Users\Erico\OneDrive\Desktop\LASTPROJECTMAP\fullrandomMappython.py", line 118, in main
room1 = createMap(mapSize)
File "C:\Users\Erico\OneDrive\Desktop\LASTPROJECTMAP\fullrandomMappython.py", line 18, in createMap
weightedBiome = random.choices(symbolList, weights, k=1)
File "C:\Users\Erico\AppData\Local\Programs\Python\Python311\Lib\random.py", line 506, in choices
raise ValueError('The number of weights does not match the population')
ValueError: The number of weights does not match the population

stray vortex
#

symbolList isn't the same length as weights list

#

weightedBiome = random.choices(symbolList, weights, k=1)

This line's causing the error

#

Check the list lengths

light haven
#

the symbolList has 5 things in it and so do the weights no?

#

did i add an extra in one of the lists by accident?

#

i dont see any'

#

weights is equal to (1, 1, 1, 1, 1)

#

which is also 5 things in the list

stray vortex
#

Print the variable, and check if it actually is 5 items

#

You're using a lot of += in the fillmap function

light haven
#

oh is that not doing what i think its doing?

#

i thought it would go (1,1,1,1,1) + (1,1,1,1,1) = (2,2,2,2,2)

stray vortex
#

!exec

x = (1, 2, 3, 4, 5)
y = (6, 7, 8, 9, 10)

x += y

print(x)
warm cedarBOT
light haven
#

welp yea i jsut saw it do that

#

when i printed it

#

should i make every weight a variable and add those?

#

something like this?
firstWeight = 1
secondWeight = 1
thirdWeight = 1
fourthWeight = 1
fifthWeight = 1
weights = (firstWeight,secondWeight,thirdWeight,fourthWeight,fifthWeight)

#

that looks so bad lmao

#

this does help me solve my second querstion though