Count increments by one too many. Input printed too many times. Python











up vote
-1
down vote

favorite












def key(shift):
data =
string = input("Please enter the string you wish to decode.n")
for i in string:
if i.strip() and i in ALPHABET:
data.append(ALPHABET[(ALPHABET.index(i) - shift) % 26])
else:
data.append(i)
output = ''.join(data)
return output

def run():

data =
count = 0
shift = 0
for shift in range (26):
count +=1
if key(shift) == "hello world":
print("Decoded.")
else:
print("Not this time!")
print(count)
print(key(shift))


When I run my program, it should execute "Decoded" at count 3, since it takes 3 shifts to decode khoor zruog into hello world. Moreover, the input "Please enter the string you wish to decode" should be printed once, rather than so many times.



Please enter the string you wish to decode.
khoor zruog
Not this time!
1
Please enter the string you wish to decode.
khoor zruog
khoor zruog
Please enter the string you wish to decode.
khoor zruog
Not this time!
2
Please enter the string you wish to decode.
khoor zruog
jgnnq yqtnf
Please enter the string you wish to decode.
khoor zruog
Not this time!
3
Please enter the string you wish to decode.
khoor zruog
ifmmp xpsme
Please enter the string you wish to decode.
khoor zruog
Decoded.
4
Please enter the string you wish to decode.
khoor zruog
hello world
Please enter the string you wish to decode.


This is what happens when I try to execute the code. I don't know why it is incrementing to 0, and I don't know why it is asking me the input so many times. Can anyone help me please?



EDIT:



def key(shift,string):
data =
for i in string:
if i.strip() and i in ALPHABET:
data.append(ALPHABET[(ALPHABET.index(i) - shift) % 26])
else:
data.append(i)
output = ''.join(data)
return output

def run():
data =
string = input("Please enter the string you wish to decode.n")
plaintext = input("Please enter plaintext word.n")
count = 0
shift = 1
for shift in range (26):
count +=1
if plaintext in key(shift,string):
print(key(shift,string))
print("The key is: ", count)
print("Decoded.")
break
else:
print(key(shift,string))
print("Not this time!")
print(count)









share|improve this question
























  • You told it to repeat the loop 26 times. If you want to exit early, look up the break statement -- which is included in your materials on looping.
    – Prune
    Nov 19 at 21:49










  • You were right, thank you so much!
    – Johnathan
    Nov 19 at 21:52










  • Regarding your last point, your statement string = input("Please enter... is inside the key() function, which means that it will request input everytime you call key(shift), which you do in your if block and again in your print() statement. Consider moving the assignment out of your loop, or your input call outside the looped function
    – G. Anderson
    Nov 19 at 21:54










  • Edited post with updated code. Another issue, it is now telling me count is 4 when I run the program. Can you please help?
    – Johnathan
    Nov 19 at 22:11

















up vote
-1
down vote

favorite












def key(shift):
data =
string = input("Please enter the string you wish to decode.n")
for i in string:
if i.strip() and i in ALPHABET:
data.append(ALPHABET[(ALPHABET.index(i) - shift) % 26])
else:
data.append(i)
output = ''.join(data)
return output

def run():

data =
count = 0
shift = 0
for shift in range (26):
count +=1
if key(shift) == "hello world":
print("Decoded.")
else:
print("Not this time!")
print(count)
print(key(shift))


When I run my program, it should execute "Decoded" at count 3, since it takes 3 shifts to decode khoor zruog into hello world. Moreover, the input "Please enter the string you wish to decode" should be printed once, rather than so many times.



Please enter the string you wish to decode.
khoor zruog
Not this time!
1
Please enter the string you wish to decode.
khoor zruog
khoor zruog
Please enter the string you wish to decode.
khoor zruog
Not this time!
2
Please enter the string you wish to decode.
khoor zruog
jgnnq yqtnf
Please enter the string you wish to decode.
khoor zruog
Not this time!
3
Please enter the string you wish to decode.
khoor zruog
ifmmp xpsme
Please enter the string you wish to decode.
khoor zruog
Decoded.
4
Please enter the string you wish to decode.
khoor zruog
hello world
Please enter the string you wish to decode.


This is what happens when I try to execute the code. I don't know why it is incrementing to 0, and I don't know why it is asking me the input so many times. Can anyone help me please?



EDIT:



def key(shift,string):
data =
for i in string:
if i.strip() and i in ALPHABET:
data.append(ALPHABET[(ALPHABET.index(i) - shift) % 26])
else:
data.append(i)
output = ''.join(data)
return output

def run():
data =
string = input("Please enter the string you wish to decode.n")
plaintext = input("Please enter plaintext word.n")
count = 0
shift = 1
for shift in range (26):
count +=1
if plaintext in key(shift,string):
print(key(shift,string))
print("The key is: ", count)
print("Decoded.")
break
else:
print(key(shift,string))
print("Not this time!")
print(count)









share|improve this question
























  • You told it to repeat the loop 26 times. If you want to exit early, look up the break statement -- which is included in your materials on looping.
    – Prune
    Nov 19 at 21:49










  • You were right, thank you so much!
    – Johnathan
    Nov 19 at 21:52










  • Regarding your last point, your statement string = input("Please enter... is inside the key() function, which means that it will request input everytime you call key(shift), which you do in your if block and again in your print() statement. Consider moving the assignment out of your loop, or your input call outside the looped function
    – G. Anderson
    Nov 19 at 21:54










  • Edited post with updated code. Another issue, it is now telling me count is 4 when I run the program. Can you please help?
    – Johnathan
    Nov 19 at 22:11















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











def key(shift):
data =
string = input("Please enter the string you wish to decode.n")
for i in string:
if i.strip() and i in ALPHABET:
data.append(ALPHABET[(ALPHABET.index(i) - shift) % 26])
else:
data.append(i)
output = ''.join(data)
return output

def run():

data =
count = 0
shift = 0
for shift in range (26):
count +=1
if key(shift) == "hello world":
print("Decoded.")
else:
print("Not this time!")
print(count)
print(key(shift))


When I run my program, it should execute "Decoded" at count 3, since it takes 3 shifts to decode khoor zruog into hello world. Moreover, the input "Please enter the string you wish to decode" should be printed once, rather than so many times.



Please enter the string you wish to decode.
khoor zruog
Not this time!
1
Please enter the string you wish to decode.
khoor zruog
khoor zruog
Please enter the string you wish to decode.
khoor zruog
Not this time!
2
Please enter the string you wish to decode.
khoor zruog
jgnnq yqtnf
Please enter the string you wish to decode.
khoor zruog
Not this time!
3
Please enter the string you wish to decode.
khoor zruog
ifmmp xpsme
Please enter the string you wish to decode.
khoor zruog
Decoded.
4
Please enter the string you wish to decode.
khoor zruog
hello world
Please enter the string you wish to decode.


This is what happens when I try to execute the code. I don't know why it is incrementing to 0, and I don't know why it is asking me the input so many times. Can anyone help me please?



EDIT:



def key(shift,string):
data =
for i in string:
if i.strip() and i in ALPHABET:
data.append(ALPHABET[(ALPHABET.index(i) - shift) % 26])
else:
data.append(i)
output = ''.join(data)
return output

def run():
data =
string = input("Please enter the string you wish to decode.n")
plaintext = input("Please enter plaintext word.n")
count = 0
shift = 1
for shift in range (26):
count +=1
if plaintext in key(shift,string):
print(key(shift,string))
print("The key is: ", count)
print("Decoded.")
break
else:
print(key(shift,string))
print("Not this time!")
print(count)









share|improve this question















def key(shift):
data =
string = input("Please enter the string you wish to decode.n")
for i in string:
if i.strip() and i in ALPHABET:
data.append(ALPHABET[(ALPHABET.index(i) - shift) % 26])
else:
data.append(i)
output = ''.join(data)
return output

def run():

data =
count = 0
shift = 0
for shift in range (26):
count +=1
if key(shift) == "hello world":
print("Decoded.")
else:
print("Not this time!")
print(count)
print(key(shift))


When I run my program, it should execute "Decoded" at count 3, since it takes 3 shifts to decode khoor zruog into hello world. Moreover, the input "Please enter the string you wish to decode" should be printed once, rather than so many times.



Please enter the string you wish to decode.
khoor zruog
Not this time!
1
Please enter the string you wish to decode.
khoor zruog
khoor zruog
Please enter the string you wish to decode.
khoor zruog
Not this time!
2
Please enter the string you wish to decode.
khoor zruog
jgnnq yqtnf
Please enter the string you wish to decode.
khoor zruog
Not this time!
3
Please enter the string you wish to decode.
khoor zruog
ifmmp xpsme
Please enter the string you wish to decode.
khoor zruog
Decoded.
4
Please enter the string you wish to decode.
khoor zruog
hello world
Please enter the string you wish to decode.


This is what happens when I try to execute the code. I don't know why it is incrementing to 0, and I don't know why it is asking me the input so many times. Can anyone help me please?



EDIT:



def key(shift,string):
data =
for i in string:
if i.strip() and i in ALPHABET:
data.append(ALPHABET[(ALPHABET.index(i) - shift) % 26])
else:
data.append(i)
output = ''.join(data)
return output

def run():
data =
string = input("Please enter the string you wish to decode.n")
plaintext = input("Please enter plaintext word.n")
count = 0
shift = 1
for shift in range (26):
count +=1
if plaintext in key(shift,string):
print(key(shift,string))
print("The key is: ", count)
print("Decoded.")
break
else:
print(key(shift,string))
print("Not this time!")
print(count)






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 22:11

























asked Nov 19 at 21:46









Johnathan

43




43












  • You told it to repeat the loop 26 times. If you want to exit early, look up the break statement -- which is included in your materials on looping.
    – Prune
    Nov 19 at 21:49










  • You were right, thank you so much!
    – Johnathan
    Nov 19 at 21:52










  • Regarding your last point, your statement string = input("Please enter... is inside the key() function, which means that it will request input everytime you call key(shift), which you do in your if block and again in your print() statement. Consider moving the assignment out of your loop, or your input call outside the looped function
    – G. Anderson
    Nov 19 at 21:54










  • Edited post with updated code. Another issue, it is now telling me count is 4 when I run the program. Can you please help?
    – Johnathan
    Nov 19 at 22:11




















  • You told it to repeat the loop 26 times. If you want to exit early, look up the break statement -- which is included in your materials on looping.
    – Prune
    Nov 19 at 21:49










  • You were right, thank you so much!
    – Johnathan
    Nov 19 at 21:52










  • Regarding your last point, your statement string = input("Please enter... is inside the key() function, which means that it will request input everytime you call key(shift), which you do in your if block and again in your print() statement. Consider moving the assignment out of your loop, or your input call outside the looped function
    – G. Anderson
    Nov 19 at 21:54










  • Edited post with updated code. Another issue, it is now telling me count is 4 when I run the program. Can you please help?
    – Johnathan
    Nov 19 at 22:11


















You told it to repeat the loop 26 times. If you want to exit early, look up the break statement -- which is included in your materials on looping.
– Prune
Nov 19 at 21:49




You told it to repeat the loop 26 times. If you want to exit early, look up the break statement -- which is included in your materials on looping.
– Prune
Nov 19 at 21:49












You were right, thank you so much!
– Johnathan
Nov 19 at 21:52




You were right, thank you so much!
– Johnathan
Nov 19 at 21:52












Regarding your last point, your statement string = input("Please enter... is inside the key() function, which means that it will request input everytime you call key(shift), which you do in your if block and again in your print() statement. Consider moving the assignment out of your loop, or your input call outside the looped function
– G. Anderson
Nov 19 at 21:54




Regarding your last point, your statement string = input("Please enter... is inside the key() function, which means that it will request input everytime you call key(shift), which you do in your if block and again in your print() statement. Consider moving the assignment out of your loop, or your input call outside the looped function
– G. Anderson
Nov 19 at 21:54












Edited post with updated code. Another issue, it is now telling me count is 4 when I run the program. Can you please help?
– Johnathan
Nov 19 at 22:11






Edited post with updated code. Another issue, it is now telling me count is 4 when I run the program. Can you please help?
– Johnathan
Nov 19 at 22:11














1 Answer
1






active

oldest

votes

















up vote
0
down vote













your string



string = input("Please enter the string you wish to decode.n")


is inside the function which is called inside the for loop. that's why you are getting print statement multiple times!



remove that input statement and your decoded problem will get solved.






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%2f53383096%2fcount-increments-by-one-too-many-input-printed-too-many-times-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    your string



    string = input("Please enter the string you wish to decode.n")


    is inside the function which is called inside the for loop. that's why you are getting print statement multiple times!



    remove that input statement and your decoded problem will get solved.






    share|improve this answer

























      up vote
      0
      down vote













      your string



      string = input("Please enter the string you wish to decode.n")


      is inside the function which is called inside the for loop. that's why you are getting print statement multiple times!



      remove that input statement and your decoded problem will get solved.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        your string



        string = input("Please enter the string you wish to decode.n")


        is inside the function which is called inside the for loop. that's why you are getting print statement multiple times!



        remove that input statement and your decoded problem will get solved.






        share|improve this answer












        your string



        string = input("Please enter the string you wish to decode.n")


        is inside the function which is called inside the for loop. that's why you are getting print statement multiple times!



        remove that input statement and your decoded problem will get solved.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 21:57









        Abhishek Velankar

        11215




        11215






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383096%2fcount-increments-by-one-too-many-input-printed-too-many-times-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

            Tonle Sap (See)

            I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

            Guatemaltekische Davis-Cup-Mannschaft