How to iterate through every word several times in python?









up vote
0
down vote

favorite












I'm making a script that should take in a set of words in a file, encrypt them in 25 different ways, then output the results to another file.



So what i have so far is a script that takes in all the words, encrypt them only once and outputs the list. I can't figure out how to encrypt each word 25 times (meaning, making 25 new words out of each word)



Here is my code so far:



for c in range(len(text)):
lister = text[c]
s += 1
print("Cipher number %s: " % c + encrypt(lister, s))
output_file.write("n")
output_file.write(encrypt(lister, s))


text is the file containing the words, and the function encrypt takes in that list, and s is the number of shifts for the encryption, meaning s = 1 is one encryption way, and s = 2 is another way to encrypt the same word. The code right now encrypts all the words in different encryptions since s changes its value each time the for loop goes through a new word



How can i make the for loop change the value of s only after it has encrypted the previous word 25 times at s = 1 to s = 25?










share|improve this question





















  • As far as I understand, 's' is adding either the digit 1 to the text or the digit 1 refers to some cipher which has the key 1? Is that correct? If so, can you increment 's' and run 's' until the incrementation reaches 25?
    – Alex_P
    Nov 10 at 19:30














up vote
0
down vote

favorite












I'm making a script that should take in a set of words in a file, encrypt them in 25 different ways, then output the results to another file.



So what i have so far is a script that takes in all the words, encrypt them only once and outputs the list. I can't figure out how to encrypt each word 25 times (meaning, making 25 new words out of each word)



Here is my code so far:



for c in range(len(text)):
lister = text[c]
s += 1
print("Cipher number %s: " % c + encrypt(lister, s))
output_file.write("n")
output_file.write(encrypt(lister, s))


text is the file containing the words, and the function encrypt takes in that list, and s is the number of shifts for the encryption, meaning s = 1 is one encryption way, and s = 2 is another way to encrypt the same word. The code right now encrypts all the words in different encryptions since s changes its value each time the for loop goes through a new word



How can i make the for loop change the value of s only after it has encrypted the previous word 25 times at s = 1 to s = 25?










share|improve this question





















  • As far as I understand, 's' is adding either the digit 1 to the text or the digit 1 refers to some cipher which has the key 1? Is that correct? If so, can you increment 's' and run 's' until the incrementation reaches 25?
    – Alex_P
    Nov 10 at 19:30












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm making a script that should take in a set of words in a file, encrypt them in 25 different ways, then output the results to another file.



So what i have so far is a script that takes in all the words, encrypt them only once and outputs the list. I can't figure out how to encrypt each word 25 times (meaning, making 25 new words out of each word)



Here is my code so far:



for c in range(len(text)):
lister = text[c]
s += 1
print("Cipher number %s: " % c + encrypt(lister, s))
output_file.write("n")
output_file.write(encrypt(lister, s))


text is the file containing the words, and the function encrypt takes in that list, and s is the number of shifts for the encryption, meaning s = 1 is one encryption way, and s = 2 is another way to encrypt the same word. The code right now encrypts all the words in different encryptions since s changes its value each time the for loop goes through a new word



How can i make the for loop change the value of s only after it has encrypted the previous word 25 times at s = 1 to s = 25?










share|improve this question













I'm making a script that should take in a set of words in a file, encrypt them in 25 different ways, then output the results to another file.



So what i have so far is a script that takes in all the words, encrypt them only once and outputs the list. I can't figure out how to encrypt each word 25 times (meaning, making 25 new words out of each word)



Here is my code so far:



for c in range(len(text)):
lister = text[c]
s += 1
print("Cipher number %s: " % c + encrypt(lister, s))
output_file.write("n")
output_file.write(encrypt(lister, s))


text is the file containing the words, and the function encrypt takes in that list, and s is the number of shifts for the encryption, meaning s = 1 is one encryption way, and s = 2 is another way to encrypt the same word. The code right now encrypts all the words in different encryptions since s changes its value each time the for loop goes through a new word



How can i make the for loop change the value of s only after it has encrypted the previous word 25 times at s = 1 to s = 25?







python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 19:19









MonaS

1




1











  • As far as I understand, 's' is adding either the digit 1 to the text or the digit 1 refers to some cipher which has the key 1? Is that correct? If so, can you increment 's' and run 's' until the incrementation reaches 25?
    – Alex_P
    Nov 10 at 19:30
















  • As far as I understand, 's' is adding either the digit 1 to the text or the digit 1 refers to some cipher which has the key 1? Is that correct? If so, can you increment 's' and run 's' until the incrementation reaches 25?
    – Alex_P
    Nov 10 at 19:30















As far as I understand, 's' is adding either the digit 1 to the text or the digit 1 refers to some cipher which has the key 1? Is that correct? If so, can you increment 's' and run 's' until the incrementation reaches 25?
– Alex_P
Nov 10 at 19:30




As far as I understand, 's' is adding either the digit 1 to the text or the digit 1 refers to some cipher which has the key 1? Is that correct? If so, can you increment 's' and run 's' until the incrementation reaches 25?
– Alex_P
Nov 10 at 19:30












2 Answers
2






active

oldest

votes

















up vote
1
down vote













What you are looking for is a nested loop. Simply put, you need to do a task 25 times for each word.



for c in range(len(text)):
lister = text[c]
for s in range(1, 26): #goes from 1 to 25.
print("Cipher number %s: " % c + encrypt(lister, s))
output_file.write("n")
output_file.write(encrypt(lister, s))


I should also mention that python gives us a much nicer way of iterating through lists using the "in" operator.



for lister in text:
for s in range(1, 26): #goes from 1 to 25.
print("Cipher set for word ",lister)
output_file.write("n")
output_file.write(encrypt(lister, s))


If you need both index numbers while iterating through a list, use enumerate instead.






share|improve this answer




















  • You're the man. That's exactly what i was looking for. thank you.
    – MonaS
    Nov 10 at 20:07

















up vote
0
down vote













I would use a nested loop, inside the current loop you have. Make that loop run 25 times for each time the outer loop iterates, and increase the value of s each time the inner loop iterates.



In other words, put the main body of your current loop inside for s in range(25):. That, in turn, should go inside for c in range(len(text)):. Does this help?






share|improve this answer




















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242569%2fhow-to-iterate-through-every-word-several-times-in-python%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    What you are looking for is a nested loop. Simply put, you need to do a task 25 times for each word.



    for c in range(len(text)):
    lister = text[c]
    for s in range(1, 26): #goes from 1 to 25.
    print("Cipher number %s: " % c + encrypt(lister, s))
    output_file.write("n")
    output_file.write(encrypt(lister, s))


    I should also mention that python gives us a much nicer way of iterating through lists using the "in" operator.



    for lister in text:
    for s in range(1, 26): #goes from 1 to 25.
    print("Cipher set for word ",lister)
    output_file.write("n")
    output_file.write(encrypt(lister, s))


    If you need both index numbers while iterating through a list, use enumerate instead.






    share|improve this answer




















    • You're the man. That's exactly what i was looking for. thank you.
      – MonaS
      Nov 10 at 20:07














    up vote
    1
    down vote













    What you are looking for is a nested loop. Simply put, you need to do a task 25 times for each word.



    for c in range(len(text)):
    lister = text[c]
    for s in range(1, 26): #goes from 1 to 25.
    print("Cipher number %s: " % c + encrypt(lister, s))
    output_file.write("n")
    output_file.write(encrypt(lister, s))


    I should also mention that python gives us a much nicer way of iterating through lists using the "in" operator.



    for lister in text:
    for s in range(1, 26): #goes from 1 to 25.
    print("Cipher set for word ",lister)
    output_file.write("n")
    output_file.write(encrypt(lister, s))


    If you need both index numbers while iterating through a list, use enumerate instead.






    share|improve this answer




















    • You're the man. That's exactly what i was looking for. thank you.
      – MonaS
      Nov 10 at 20:07












    up vote
    1
    down vote










    up vote
    1
    down vote









    What you are looking for is a nested loop. Simply put, you need to do a task 25 times for each word.



    for c in range(len(text)):
    lister = text[c]
    for s in range(1, 26): #goes from 1 to 25.
    print("Cipher number %s: " % c + encrypt(lister, s))
    output_file.write("n")
    output_file.write(encrypt(lister, s))


    I should also mention that python gives us a much nicer way of iterating through lists using the "in" operator.



    for lister in text:
    for s in range(1, 26): #goes from 1 to 25.
    print("Cipher set for word ",lister)
    output_file.write("n")
    output_file.write(encrypt(lister, s))


    If you need both index numbers while iterating through a list, use enumerate instead.






    share|improve this answer












    What you are looking for is a nested loop. Simply put, you need to do a task 25 times for each word.



    for c in range(len(text)):
    lister = text[c]
    for s in range(1, 26): #goes from 1 to 25.
    print("Cipher number %s: " % c + encrypt(lister, s))
    output_file.write("n")
    output_file.write(encrypt(lister, s))


    I should also mention that python gives us a much nicer way of iterating through lists using the "in" operator.



    for lister in text:
    for s in range(1, 26): #goes from 1 to 25.
    print("Cipher set for word ",lister)
    output_file.write("n")
    output_file.write(encrypt(lister, s))


    If you need both index numbers while iterating through a list, use enumerate instead.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 10 at 19:29









    Paritosh Singh

    2116




    2116











    • You're the man. That's exactly what i was looking for. thank you.
      – MonaS
      Nov 10 at 20:07
















    • You're the man. That's exactly what i was looking for. thank you.
      – MonaS
      Nov 10 at 20:07















    You're the man. That's exactly what i was looking for. thank you.
    – MonaS
    Nov 10 at 20:07




    You're the man. That's exactly what i was looking for. thank you.
    – MonaS
    Nov 10 at 20:07












    up vote
    0
    down vote













    I would use a nested loop, inside the current loop you have. Make that loop run 25 times for each time the outer loop iterates, and increase the value of s each time the inner loop iterates.



    In other words, put the main body of your current loop inside for s in range(25):. That, in turn, should go inside for c in range(len(text)):. Does this help?






    share|improve this answer
























      up vote
      0
      down vote













      I would use a nested loop, inside the current loop you have. Make that loop run 25 times for each time the outer loop iterates, and increase the value of s each time the inner loop iterates.



      In other words, put the main body of your current loop inside for s in range(25):. That, in turn, should go inside for c in range(len(text)):. Does this help?






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        I would use a nested loop, inside the current loop you have. Make that loop run 25 times for each time the outer loop iterates, and increase the value of s each time the inner loop iterates.



        In other words, put the main body of your current loop inside for s in range(25):. That, in turn, should go inside for c in range(len(text)):. Does this help?






        share|improve this answer












        I would use a nested loop, inside the current loop you have. Make that loop run 25 times for each time the outer loop iterates, and increase the value of s each time the inner loop iterates.



        In other words, put the main body of your current loop inside for s in range(25):. That, in turn, should go inside for c in range(len(text)):. Does this help?







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 19:29









        Calc-You-Later

        1




        1



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53242569%2fhow-to-iterate-through-every-word-several-times-in-python%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線