Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For loop not working
#1
I'm brand new to python and trying to figure out why my code isn't working. I'm trying to simulate the rot13 function, but my output only returns the first character despite being in a for loop?

def rot13(text, rotation):
        text = text.lower()
        
        alphabet = 'abcdefghijklmnopqrstuvwxyz'
        
        for letter in text:
            if letter == ' ':
                encrypted = ' '
            else:
                rotated = alphabet.index(letter) + int(rotation)
                if rotated > 26:
                    rotated = rotated % 26
                encrypted = alphabet[rotated]
                return encrypted

        
print(rot13('Hello there',13))
Reply
#2
Probably you want the return out of the loop...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
the loop is working, but you have a return that's executer on first pass.
use a debugger and step through your code.
Reply
#4
it's because you put the return statement directly in your for loop. instead of storing the value and continue loop. not sure what you exactly trying to do, but i'm guessing you wanted to return the encrypted message.

def rot13(text, rotation):

        ret = ''
        text = text.lower()
        alphabet = 'abcdefghijklmnopqrstuvwxyz'
         
        for letter in text:
            if letter == ' ':
                ret = ret + ' '
            else:
                rotated = alphabet.index(letter) + int(rotation)
                if rotated > 26:
                    rotated = rotated % 26
                encrypted = alphabet[rotated]
                ret = ret + encrypted
            
        return ret
 
         
print(rot13('Hello there',13))
output: uryyb gurer
Reply
#5
Oh I see, thank you!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  while loop not working-I am using sublime text editor mma_python 4 1,127 Feb-05-2023, 06:26 PM
Last Post: deanhystad
  WHILE Loop - constant variables NOT working with user input boundaries C0D3R 4 1,486 Apr-05-2022, 06:18 AM
Last Post: C0D3R
  Why is the while loop not working? mcoliver88 5 3,100 Aug-18-2020, 03:27 PM
Last Post: deanhystad
  Infinite loop not working pmp2 2 1,649 Aug-18-2020, 12:27 PM
Last Post: deanhystad
  Loop not working Nonameface 8 2,909 Jul-19-2020, 12:27 PM
Last Post: snippsat
  for loop script over telnet in Python 3.5 is not working abhijithd123 1 2,899 May-10-2020, 03:22 AM
Last Post: bowlofred
  How to keep a loop containing a web import working in python executable? coder1384 3 2,873 Feb-22-2020, 06:49 PM
Last Post: snippsat
  Appending to list not working and causing a infinite loop eiger23 8 3,999 Oct-10-2019, 03:41 PM
Last Post: eiger23
  Working on my FOR loop wilsonrivas 1 2,211 May-24-2019, 04:05 PM
Last Post: heiner55
  plotting inside the loop is not working jenya56 4 3,088 Apr-10-2019, 08:11 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020