How to “restart” the loop so that both Python lists are complete before the program ends?
I am trying to create a program in which the user enters 3 fruits and 3 nonfruits into two different lists.
The user first chooses the first list by typing in "fruits" or "nonfruits".
The user then enters each qualifying item until the first list is full.
My issue is that once the first selected list is full, the program ends.
I want the user to be prompted to enter in data into the other list until it also is full.
I thought that adding in the "while len(fruits) < 3 and len(notfruits) < 3:" would work, but it doesn't seem to make a difference.
How can I do this?
fruits =
notfruits =
print(fruits)
print(notfruits)
print("Please enter fruits or notfruits:")
y = str(input(": "))
while len(fruits) < 3 and len(notfruits) < 3:
if y == "fruits":
while len(fruits) < 3:
x = str(input(": "))
x = x.strip()
if x in notfruits:
print(x + " is not a fruit!")
elif x in fruits:
print(x + " is already in the list!")
else:
fruits.append(x)
print(fruits)
elif y == "notfruits":
while len(notfruits) < 3:
x = str(input(": "))
x = x.strip()
if x in fruits:
print(x + " is a fruit!")
elif x in notfruits:
print(x + " is already in the list!")
else:
notfruits.append(x)
print(notfruits)
else:
print("Not a valid option!")
python loops
add a comment |
I am trying to create a program in which the user enters 3 fruits and 3 nonfruits into two different lists.
The user first chooses the first list by typing in "fruits" or "nonfruits".
The user then enters each qualifying item until the first list is full.
My issue is that once the first selected list is full, the program ends.
I want the user to be prompted to enter in data into the other list until it also is full.
I thought that adding in the "while len(fruits) < 3 and len(notfruits) < 3:" would work, but it doesn't seem to make a difference.
How can I do this?
fruits =
notfruits =
print(fruits)
print(notfruits)
print("Please enter fruits or notfruits:")
y = str(input(": "))
while len(fruits) < 3 and len(notfruits) < 3:
if y == "fruits":
while len(fruits) < 3:
x = str(input(": "))
x = x.strip()
if x in notfruits:
print(x + " is not a fruit!")
elif x in fruits:
print(x + " is already in the list!")
else:
fruits.append(x)
print(fruits)
elif y == "notfruits":
while len(notfruits) < 3:
x = str(input(": "))
x = x.strip()
if x in fruits:
print(x + " is a fruit!")
elif x in notfruits:
print(x + " is already in the list!")
else:
notfruits.append(x)
print(notfruits)
else:
print("Not a valid option!")
python loops
To repeatedly enter items for a list, you put the code to accept an entry in a loop. So to repeatedly accept entries for different lists, put the code to fill a single list within a loop.
– Scott Hunter
Nov 12 at 20:07
Hi, didn't read all the code but when you say in the codewhile len(fruits) < 3 and len(notfruits) < 3:
the condition would skip whenever fruits becomes full: len(fruits) = 3, so the code should rather bewhile len(fruits) < 3 or len(notfruits) < 3:
.
– ankabout
Nov 12 at 20:13
add a comment |
I am trying to create a program in which the user enters 3 fruits and 3 nonfruits into two different lists.
The user first chooses the first list by typing in "fruits" or "nonfruits".
The user then enters each qualifying item until the first list is full.
My issue is that once the first selected list is full, the program ends.
I want the user to be prompted to enter in data into the other list until it also is full.
I thought that adding in the "while len(fruits) < 3 and len(notfruits) < 3:" would work, but it doesn't seem to make a difference.
How can I do this?
fruits =
notfruits =
print(fruits)
print(notfruits)
print("Please enter fruits or notfruits:")
y = str(input(": "))
while len(fruits) < 3 and len(notfruits) < 3:
if y == "fruits":
while len(fruits) < 3:
x = str(input(": "))
x = x.strip()
if x in notfruits:
print(x + " is not a fruit!")
elif x in fruits:
print(x + " is already in the list!")
else:
fruits.append(x)
print(fruits)
elif y == "notfruits":
while len(notfruits) < 3:
x = str(input(": "))
x = x.strip()
if x in fruits:
print(x + " is a fruit!")
elif x in notfruits:
print(x + " is already in the list!")
else:
notfruits.append(x)
print(notfruits)
else:
print("Not a valid option!")
python loops
I am trying to create a program in which the user enters 3 fruits and 3 nonfruits into two different lists.
The user first chooses the first list by typing in "fruits" or "nonfruits".
The user then enters each qualifying item until the first list is full.
My issue is that once the first selected list is full, the program ends.
I want the user to be prompted to enter in data into the other list until it also is full.
I thought that adding in the "while len(fruits) < 3 and len(notfruits) < 3:" would work, but it doesn't seem to make a difference.
How can I do this?
fruits =
notfruits =
print(fruits)
print(notfruits)
print("Please enter fruits or notfruits:")
y = str(input(": "))
while len(fruits) < 3 and len(notfruits) < 3:
if y == "fruits":
while len(fruits) < 3:
x = str(input(": "))
x = x.strip()
if x in notfruits:
print(x + " is not a fruit!")
elif x in fruits:
print(x + " is already in the list!")
else:
fruits.append(x)
print(fruits)
elif y == "notfruits":
while len(notfruits) < 3:
x = str(input(": "))
x = x.strip()
if x in fruits:
print(x + " is a fruit!")
elif x in notfruits:
print(x + " is already in the list!")
else:
notfruits.append(x)
print(notfruits)
else:
print("Not a valid option!")
python loops
python loops
edited Nov 12 at 20:07
asked Nov 12 at 20:04
Bobert
284
284
To repeatedly enter items for a list, you put the code to accept an entry in a loop. So to repeatedly accept entries for different lists, put the code to fill a single list within a loop.
– Scott Hunter
Nov 12 at 20:07
Hi, didn't read all the code but when you say in the codewhile len(fruits) < 3 and len(notfruits) < 3:
the condition would skip whenever fruits becomes full: len(fruits) = 3, so the code should rather bewhile len(fruits) < 3 or len(notfruits) < 3:
.
– ankabout
Nov 12 at 20:13
add a comment |
To repeatedly enter items for a list, you put the code to accept an entry in a loop. So to repeatedly accept entries for different lists, put the code to fill a single list within a loop.
– Scott Hunter
Nov 12 at 20:07
Hi, didn't read all the code but when you say in the codewhile len(fruits) < 3 and len(notfruits) < 3:
the condition would skip whenever fruits becomes full: len(fruits) = 3, so the code should rather bewhile len(fruits) < 3 or len(notfruits) < 3:
.
– ankabout
Nov 12 at 20:13
To repeatedly enter items for a list, you put the code to accept an entry in a loop. So to repeatedly accept entries for different lists, put the code to fill a single list within a loop.
– Scott Hunter
Nov 12 at 20:07
To repeatedly enter items for a list, you put the code to accept an entry in a loop. So to repeatedly accept entries for different lists, put the code to fill a single list within a loop.
– Scott Hunter
Nov 12 at 20:07
Hi, didn't read all the code but when you say in the code
while len(fruits) < 3 and len(notfruits) < 3:
the condition would skip whenever fruits becomes full: len(fruits) = 3, so the code should rather be while len(fruits) < 3 or len(notfruits) < 3:
.– ankabout
Nov 12 at 20:13
Hi, didn't read all the code but when you say in the code
while len(fruits) < 3 and len(notfruits) < 3:
the condition would skip whenever fruits becomes full: len(fruits) = 3, so the code should rather be while len(fruits) < 3 or len(notfruits) < 3:
.– ankabout
Nov 12 at 20:13
add a comment |
1 Answer
1
active
oldest
votes
- Consider using
or
instead ofand
- Move the input part inside the loop, or else
y
will never change
Here's what I mean:
fruits =
notfruits =
print(fruits)
print(notfruits)
while len(fruits) < 3 or len(notfruits) < 3: # replaced `and` with `or`
print("Please enter fruits or notfruits:") #
y = str(input(": ")) # moved the input here
if y == "fruits":
while len(fruits) < 3:
x = str(input(": "))
x = x.strip()
if x in notfruits:
print(x + " is not a fruit!")
elif x in fruits:
print(x + " is already in the list!")
else:
fruits.append(x)
print(fruits)
elif y == "notfruits":
while len(notfruits) < 3:
x = str(input(": "))
x = x.strip()
if x in fruits:
print(x + " is a fruit!")
elif x in notfruits:
print(x + " is already in the list!")
else:
notfruits.append(x)
print(notfruits)
else:
print("Not a valid option!")
Thanks! Exactly what I was looking for. I am a total noob when it comes to python, still in the process of learning it, and my loop knowledge is lacking.
– Bobert
Nov 12 at 22:15
add a comment |
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%2f53269313%2fhow-to-restart-the-loop-so-that-both-python-lists-are-complete-before-the-prog%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
- Consider using
or
instead ofand
- Move the input part inside the loop, or else
y
will never change
Here's what I mean:
fruits =
notfruits =
print(fruits)
print(notfruits)
while len(fruits) < 3 or len(notfruits) < 3: # replaced `and` with `or`
print("Please enter fruits or notfruits:") #
y = str(input(": ")) # moved the input here
if y == "fruits":
while len(fruits) < 3:
x = str(input(": "))
x = x.strip()
if x in notfruits:
print(x + " is not a fruit!")
elif x in fruits:
print(x + " is already in the list!")
else:
fruits.append(x)
print(fruits)
elif y == "notfruits":
while len(notfruits) < 3:
x = str(input(": "))
x = x.strip()
if x in fruits:
print(x + " is a fruit!")
elif x in notfruits:
print(x + " is already in the list!")
else:
notfruits.append(x)
print(notfruits)
else:
print("Not a valid option!")
Thanks! Exactly what I was looking for. I am a total noob when it comes to python, still in the process of learning it, and my loop knowledge is lacking.
– Bobert
Nov 12 at 22:15
add a comment |
- Consider using
or
instead ofand
- Move the input part inside the loop, or else
y
will never change
Here's what I mean:
fruits =
notfruits =
print(fruits)
print(notfruits)
while len(fruits) < 3 or len(notfruits) < 3: # replaced `and` with `or`
print("Please enter fruits or notfruits:") #
y = str(input(": ")) # moved the input here
if y == "fruits":
while len(fruits) < 3:
x = str(input(": "))
x = x.strip()
if x in notfruits:
print(x + " is not a fruit!")
elif x in fruits:
print(x + " is already in the list!")
else:
fruits.append(x)
print(fruits)
elif y == "notfruits":
while len(notfruits) < 3:
x = str(input(": "))
x = x.strip()
if x in fruits:
print(x + " is a fruit!")
elif x in notfruits:
print(x + " is already in the list!")
else:
notfruits.append(x)
print(notfruits)
else:
print("Not a valid option!")
Thanks! Exactly what I was looking for. I am a total noob when it comes to python, still in the process of learning it, and my loop knowledge is lacking.
– Bobert
Nov 12 at 22:15
add a comment |
- Consider using
or
instead ofand
- Move the input part inside the loop, or else
y
will never change
Here's what I mean:
fruits =
notfruits =
print(fruits)
print(notfruits)
while len(fruits) < 3 or len(notfruits) < 3: # replaced `and` with `or`
print("Please enter fruits or notfruits:") #
y = str(input(": ")) # moved the input here
if y == "fruits":
while len(fruits) < 3:
x = str(input(": "))
x = x.strip()
if x in notfruits:
print(x + " is not a fruit!")
elif x in fruits:
print(x + " is already in the list!")
else:
fruits.append(x)
print(fruits)
elif y == "notfruits":
while len(notfruits) < 3:
x = str(input(": "))
x = x.strip()
if x in fruits:
print(x + " is a fruit!")
elif x in notfruits:
print(x + " is already in the list!")
else:
notfruits.append(x)
print(notfruits)
else:
print("Not a valid option!")
- Consider using
or
instead ofand
- Move the input part inside the loop, or else
y
will never change
Here's what I mean:
fruits =
notfruits =
print(fruits)
print(notfruits)
while len(fruits) < 3 or len(notfruits) < 3: # replaced `and` with `or`
print("Please enter fruits or notfruits:") #
y = str(input(": ")) # moved the input here
if y == "fruits":
while len(fruits) < 3:
x = str(input(": "))
x = x.strip()
if x in notfruits:
print(x + " is not a fruit!")
elif x in fruits:
print(x + " is already in the list!")
else:
fruits.append(x)
print(fruits)
elif y == "notfruits":
while len(notfruits) < 3:
x = str(input(": "))
x = x.strip()
if x in fruits:
print(x + " is a fruit!")
elif x in notfruits:
print(x + " is already in the list!")
else:
notfruits.append(x)
print(notfruits)
else:
print("Not a valid option!")
answered Nov 12 at 20:20
Ayxan
1,512115
1,512115
Thanks! Exactly what I was looking for. I am a total noob when it comes to python, still in the process of learning it, and my loop knowledge is lacking.
– Bobert
Nov 12 at 22:15
add a comment |
Thanks! Exactly what I was looking for. I am a total noob when it comes to python, still in the process of learning it, and my loop knowledge is lacking.
– Bobert
Nov 12 at 22:15
Thanks! Exactly what I was looking for. I am a total noob when it comes to python, still in the process of learning it, and my loop knowledge is lacking.
– Bobert
Nov 12 at 22:15
Thanks! Exactly what I was looking for. I am a total noob when it comes to python, still in the process of learning it, and my loop knowledge is lacking.
– Bobert
Nov 12 at 22:15
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%2f53269313%2fhow-to-restart-the-loop-so-that-both-python-lists-are-complete-before-the-prog%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
To repeatedly enter items for a list, you put the code to accept an entry in a loop. So to repeatedly accept entries for different lists, put the code to fill a single list within a loop.
– Scott Hunter
Nov 12 at 20:07
Hi, didn't read all the code but when you say in the code
while len(fruits) < 3 and len(notfruits) < 3:
the condition would skip whenever fruits becomes full: len(fruits) = 3, so the code should rather bewhile len(fruits) < 3 or len(notfruits) < 3:
.– ankabout
Nov 12 at 20:13