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)
python
add a comment |
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)
python
You told it to repeat the loop 26 times. If you want to exit early, look up thebreakstatement -- 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 statementstring = input("Please enter...is inside thekey()function, which means that it will request input everytime you callkey(shift), which you do in yourifblock and again in yourprint()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
add a comment |
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)
python
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
python
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 thebreakstatement -- 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 statementstring = input("Please enter...is inside thekey()function, which means that it will request input everytime you callkey(shift), which you do in yourifblock and again in yourprint()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
add a comment |
You told it to repeat the loop 26 times. If you want to exit early, look up thebreakstatement -- 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 statementstring = input("Please enter...is inside thekey()function, which means that it will request input everytime you callkey(shift), which you do in yourifblock and again in yourprint()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
add a comment |
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.
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 19 at 21:57
Abhishek Velankar
11215
11215
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You told it to repeat the loop 26 times. If you want to exit early, look up the
breakstatement -- 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 thekey()function, which means that it will request input everytime you callkey(shift), which you do in yourifblock and again in yourprint()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