Why is this loop repeating each string multiple times?
I'm in an intro to python class and am having some trouble understanding the boolean loop situation I've gotten myself into. I've tried searching for a similar situation but so far I haven't found any answers which are close to what I'm doing (or at least that I could see the similarity :/) so I'm asking for help. The zybook is inputting 4 strings of names and my last loop is looping each one multiple times (over 5000 apparently). I don't know how to make it only do each string once...I keep trying different fixes but so far I just break other parts of the code which are functioning. I'd appreciate any insight into how I can fix this problem. Thanks so much! Here is the code:
user_bool = "true"
user_input = input("Enter input string:n")
while user_bool != "false":
if "," not in user_input:
print ("Error: No comma in string.n")
user_input = input("Enter input string:n")
user_bool = "true"
else:
user_bool = "false"
continue
s_bool = "true"
while s_bool != "false":
if ',' in user_input:
s_input = user_input.split(",")
print ("First word:",s_input[0].strip())
print ("Second word:",s_input[1].strip())
print ("n")
s_bool = "true"
else:
s_bool = "false"
break
It's not giving me an error message other than I have an open loop so it's repeating over 5000 times.
python-3.x loops boolean
|
show 3 more comments
I'm in an intro to python class and am having some trouble understanding the boolean loop situation I've gotten myself into. I've tried searching for a similar situation but so far I haven't found any answers which are close to what I'm doing (or at least that I could see the similarity :/) so I'm asking for help. The zybook is inputting 4 strings of names and my last loop is looping each one multiple times (over 5000 apparently). I don't know how to make it only do each string once...I keep trying different fixes but so far I just break other parts of the code which are functioning. I'd appreciate any insight into how I can fix this problem. Thanks so much! Here is the code:
user_bool = "true"
user_input = input("Enter input string:n")
while user_bool != "false":
if "," not in user_input:
print ("Error: No comma in string.n")
user_input = input("Enter input string:n")
user_bool = "true"
else:
user_bool = "false"
continue
s_bool = "true"
while s_bool != "false":
if ',' in user_input:
s_input = user_input.split(",")
print ("First word:",s_input[0].strip())
print ("Second word:",s_input[1].strip())
print ("n")
s_bool = "true"
else:
s_bool = "false"
break
It's not giving me an error message other than I have an open loop so it's repeating over 5000 times.
python-3.x loops boolean
1
Is there a reason for using strings with the contents"true"
and"false"
instead of the boolean valuesTrue
andFalse
built into the language?
– ggorlen
Nov 23 '18 at 18:35
Hmm well I was getting errors and found an example of a similar problem which had true and false written that way. When I changed my code (in other ways) I just changed that too and things started working so I thought I'd been doing it wrong. I'll change it and see if that makes any difference. Thanks!
– Amorky
Nov 23 '18 at 18:42
@ggorlen the input() function would return a string though.
– gilch
Nov 23 '18 at 18:49
@gilch I don't see why that matters. No boolean comparisons are being done against the input string.
– ggorlen
Nov 23 '18 at 18:51
In the current version of the code, that is true, but I'd assume an earlier iteration did that. I'm not saying it should be done that way now, just that that is probably why Amorky used a string in the first place.
– gilch
Nov 23 '18 at 18:53
|
show 3 more comments
I'm in an intro to python class and am having some trouble understanding the boolean loop situation I've gotten myself into. I've tried searching for a similar situation but so far I haven't found any answers which are close to what I'm doing (or at least that I could see the similarity :/) so I'm asking for help. The zybook is inputting 4 strings of names and my last loop is looping each one multiple times (over 5000 apparently). I don't know how to make it only do each string once...I keep trying different fixes but so far I just break other parts of the code which are functioning. I'd appreciate any insight into how I can fix this problem. Thanks so much! Here is the code:
user_bool = "true"
user_input = input("Enter input string:n")
while user_bool != "false":
if "," not in user_input:
print ("Error: No comma in string.n")
user_input = input("Enter input string:n")
user_bool = "true"
else:
user_bool = "false"
continue
s_bool = "true"
while s_bool != "false":
if ',' in user_input:
s_input = user_input.split(",")
print ("First word:",s_input[0].strip())
print ("Second word:",s_input[1].strip())
print ("n")
s_bool = "true"
else:
s_bool = "false"
break
It's not giving me an error message other than I have an open loop so it's repeating over 5000 times.
python-3.x loops boolean
I'm in an intro to python class and am having some trouble understanding the boolean loop situation I've gotten myself into. I've tried searching for a similar situation but so far I haven't found any answers which are close to what I'm doing (or at least that I could see the similarity :/) so I'm asking for help. The zybook is inputting 4 strings of names and my last loop is looping each one multiple times (over 5000 apparently). I don't know how to make it only do each string once...I keep trying different fixes but so far I just break other parts of the code which are functioning. I'd appreciate any insight into how I can fix this problem. Thanks so much! Here is the code:
user_bool = "true"
user_input = input("Enter input string:n")
while user_bool != "false":
if "," not in user_input:
print ("Error: No comma in string.n")
user_input = input("Enter input string:n")
user_bool = "true"
else:
user_bool = "false"
continue
s_bool = "true"
while s_bool != "false":
if ',' in user_input:
s_input = user_input.split(",")
print ("First word:",s_input[0].strip())
print ("Second word:",s_input[1].strip())
print ("n")
s_bool = "true"
else:
s_bool = "false"
break
It's not giving me an error message other than I have an open loop so it's repeating over 5000 times.
python-3.x loops boolean
python-3.x loops boolean
edited Nov 23 '18 at 18:44
Amorky
asked Nov 23 '18 at 18:32
AmorkyAmorky
187
187
1
Is there a reason for using strings with the contents"true"
and"false"
instead of the boolean valuesTrue
andFalse
built into the language?
– ggorlen
Nov 23 '18 at 18:35
Hmm well I was getting errors and found an example of a similar problem which had true and false written that way. When I changed my code (in other ways) I just changed that too and things started working so I thought I'd been doing it wrong. I'll change it and see if that makes any difference. Thanks!
– Amorky
Nov 23 '18 at 18:42
@ggorlen the input() function would return a string though.
– gilch
Nov 23 '18 at 18:49
@gilch I don't see why that matters. No boolean comparisons are being done against the input string.
– ggorlen
Nov 23 '18 at 18:51
In the current version of the code, that is true, but I'd assume an earlier iteration did that. I'm not saying it should be done that way now, just that that is probably why Amorky used a string in the first place.
– gilch
Nov 23 '18 at 18:53
|
show 3 more comments
1
Is there a reason for using strings with the contents"true"
and"false"
instead of the boolean valuesTrue
andFalse
built into the language?
– ggorlen
Nov 23 '18 at 18:35
Hmm well I was getting errors and found an example of a similar problem which had true and false written that way. When I changed my code (in other ways) I just changed that too and things started working so I thought I'd been doing it wrong. I'll change it and see if that makes any difference. Thanks!
– Amorky
Nov 23 '18 at 18:42
@ggorlen the input() function would return a string though.
– gilch
Nov 23 '18 at 18:49
@gilch I don't see why that matters. No boolean comparisons are being done against the input string.
– ggorlen
Nov 23 '18 at 18:51
In the current version of the code, that is true, but I'd assume an earlier iteration did that. I'm not saying it should be done that way now, just that that is probably why Amorky used a string in the first place.
– gilch
Nov 23 '18 at 18:53
1
1
Is there a reason for using strings with the contents
"true"
and "false"
instead of the boolean values True
and False
built into the language?– ggorlen
Nov 23 '18 at 18:35
Is there a reason for using strings with the contents
"true"
and "false"
instead of the boolean values True
and False
built into the language?– ggorlen
Nov 23 '18 at 18:35
Hmm well I was getting errors and found an example of a similar problem which had true and false written that way. When I changed my code (in other ways) I just changed that too and things started working so I thought I'd been doing it wrong. I'll change it and see if that makes any difference. Thanks!
– Amorky
Nov 23 '18 at 18:42
Hmm well I was getting errors and found an example of a similar problem which had true and false written that way. When I changed my code (in other ways) I just changed that too and things started working so I thought I'd been doing it wrong. I'll change it and see if that makes any difference. Thanks!
– Amorky
Nov 23 '18 at 18:42
@ggorlen the input() function would return a string though.
– gilch
Nov 23 '18 at 18:49
@ggorlen the input() function would return a string though.
– gilch
Nov 23 '18 at 18:49
@gilch I don't see why that matters. No boolean comparisons are being done against the input string.
– ggorlen
Nov 23 '18 at 18:51
@gilch I don't see why that matters. No boolean comparisons are being done against the input string.
– ggorlen
Nov 23 '18 at 18:51
In the current version of the code, that is true, but I'd assume an earlier iteration did that. I'm not saying it should be done that way now, just that that is probably why Amorky used a string in the first place.
– gilch
Nov 23 '18 at 18:53
In the current version of the code, that is true, but I'd assume an earlier iteration did that. I'm not saying it should be done that way now, just that that is probably why Amorky used a string in the first place.
– gilch
Nov 23 '18 at 18:53
|
show 3 more comments
0
active
oldest
votes
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53451509%2fwhy-is-this-loop-repeating-each-string-multiple-times%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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%2f53451509%2fwhy-is-this-loop-repeating-each-string-multiple-times%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
1
Is there a reason for using strings with the contents
"true"
and"false"
instead of the boolean valuesTrue
andFalse
built into the language?– ggorlen
Nov 23 '18 at 18:35
Hmm well I was getting errors and found an example of a similar problem which had true and false written that way. When I changed my code (in other ways) I just changed that too and things started working so I thought I'd been doing it wrong. I'll change it and see if that makes any difference. Thanks!
– Amorky
Nov 23 '18 at 18:42
@ggorlen the input() function would return a string though.
– gilch
Nov 23 '18 at 18:49
@gilch I don't see why that matters. No boolean comparisons are being done against the input string.
– ggorlen
Nov 23 '18 at 18:51
In the current version of the code, that is true, but I'd assume an earlier iteration did that. I'm not saying it should be done that way now, just that that is probably why Amorky used a string in the first place.
– gilch
Nov 23 '18 at 18:53