#πŸ”’ beginner variable problem

37 messages Β· Page 1 of 1 (latest)

clever wasp
#

hi, why doesn't the resultat variable change

    n=n_mots(ch)
    for i in range(len(ch)):
        if ch[i] != " ":
            b=ord(ch[i])
            if b+n < 90:
                resultat=resultat+chr(b+n)
            else:
                resultat=resultat+chr(b+n-26)
        else:
            resultat=resultat+" "
resultat=""
crypter(ch,resultat)
print(resultat)```
tacit prawnBOT
#

Hey @clever wasp!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
tacit prawnBOT
#

@clever wasp

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

clever wasp
#
def crypter(ch,resultat):
    n=n_mots(ch)
    for i in range(len(ch)):
        if ch[i] != " ":
            b=ord(ch[i])
            if b+n < 90:
                resultat=resultat+chr(b+n)
            else:
                resultat=resultat+chr(b+n-26)
        else:
            resultat=resultat+" "
resultat=""
crypter(ch,resultat)
print(resultat)
#

immutable?

celest coral
jolly aspen
#

It means to "change" a string that you basically just create a new one and assign it to the old variable

clever wasp
#

it's a string

fathom onyx
celest coral
celest coral
tacit prawnBOT
#
Print and return

Here's a handy animation demonstrating how print and return differ in behavior.

See also: /tag return

clever wasp
#

i thought that it should change resultat

celest coral
clever wasp
#

what isn't it a procedure

celest coral
#

except for class attributes

clever wasp
#

so it doesn't need a return?

jolly aspen
#

They are expecting pass-by-reference

celest coral
#

hmm

celest coral
upbeat forge
clever wasp
#

so the problem is with strings

#

if it's an array it works?

jolly aspen
#

It's not a problem per se - it's just the way Python works

upbeat forge
#

pretty much, if you use list or dict it will work that way

clever wasp
#

because when we use array it does work i thought it's the same

#

oh okay list or array is the same thing right?

upbeat forge
#

you could say its the same

#

its not entirely true

clever wasp
#

so it's better to use a function ( return a string)

upbeat forge
#

if im not wrong: arrays allow you to hold a bunch same datatypes in a single variable but since python is a dynamic language we have list which allows us to hold an "array" of different datatypes in a single variable

clever wasp
#

oh okay it's hard to understand

#

i'll stick to what i'm told to do hah but thank you so much ☺️

upbeat forge
#

yeah alr

clever wasp
#

!close

tacit prawnBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.