rs19
rs19

Reputation: 667

How do I get this main() to work with my encoder()?

Right now it's not running the function on each letter in the sentence and then printing out the result.

#secret code encryptor 

def encoder(plain):

    for i in plain:
        i=ord(i)*77+4
        return i 

def main():

    plain=input('Enter a sentence: ')
    final=encoder(plain)
    print(final)

main()

Upvotes: 0

Views: 40

Answers (2)

kviiri
kviiri

Reputation: 3302

The error is in encoder:

def encoder(plain):

for i in plain:
    i=ord(i)*77+4
    return i

You should realize that when the return statement is executed, the execution of encoder ends right away. The body of the loop is never executed even twice! So you will want to move that return statement if you want your for loop to actually iterate through all the numbers. After that, you might run into another problem resulting from i being overwritten each round, so you will have to rethink how you construct your return value altogether.

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1121884

You need to collect all converted characters in your function in a list, and return that:

def encoder(plain):
    result = []  

    for i in plain:
        i=ord(i)*77+4
        result.append(i)

    return result 

return exits the function immediately, so your version only returns the first encoded value.

Upvotes: 1

Related Questions