#๐Ÿ”’ How is it going out of range?

38 messages ยท Page 1 of 1 (latest)

surreal anvilBOT
#

@radiant socket

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.

radiant socket
#

Also here is the code: alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
x = list(text)
c=len(x)
e=len(alphabet)
j=0
print(x)
n=0

while j<c:
for a in range(0, e-1):
if alphabet[a] in x[j]:
x[j] = alphabet[a+shift]
j+=1
n=0

print(x)

brisk marten
#

you're indexing by a + shift given shift can be as big as you like (since it's an user input) then it will break

fallow hemlock
#

if alphabet[a] is 'z', then a shift of just 1 will do this

#

you probably want to wrap around, use modulo for that: 123 % 100 == 23

brisk marten
#

or check if a + shift < len(alphabet)
that depends on what you wanna do exactly

radiant socket
#

hi

#

sry my sound was off i think

radiant socket
#

but i also inputted just abc

#

and then shift of 1 then also it was going out of range?

#

so i was just wondering why it was doing that

#

could there be an explanation for that?

#

it also said to use break function

fallow hemlock
radiant socket
#

but isn't j is getting incremented only if if the if statement is correct?

fallow hemlock
#

yes, you increment it 3 times because of the 'abc'
but then you reach 'd' in the alphabet and you check again, trying to access x[j] again, which is 'abc'[3], which is out of range

radiant socket
#

hm I'm still trying to understand

fallow hemlock
#

this could be fixed by using short-circuiting:
if j < len(x) and alphabet[a] in x[j]:

the and operator causes the latter part to not get evaluated if the first operand is false

radiant socket
#

sry if im slow

fallow hemlock
#

you can make the code easier to read, and perhaps easier to debug, if you don't use 1-letter variable names

radiant socket
#

okay , thanks:)

fallow hemlock
#

there's also no real reason to save these as variables py c=len(x) e=len(alphabet)

radiant socket
#

i think im getting it a bit now

#

yah i got that haha but i was too lazy to change it again

#

it tries to check the code again when i increment j thats why its going out of range?

fallow hemlock
#

you get 'abc'[3], which is out of range, yes

radiant socket
#

Owww

#

I think I get it now

#

Thanks: D

#

Is it done? I just leave it like this?

fallow hemlock
#

write !close if you're sure there's nothing else

radiant socket
#

Okayyy ty:)

#

Have a great day!

#

!close

surreal anvilBOT
#
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.