How to “restart” the loop so that both Python lists are complete before the program ends?












1














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!")









share|improve this question
























  • 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
















1














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!")









share|improve this question
























  • 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














1












1








1







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!")









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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


















  • 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
















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












1 Answer
1






active

oldest

votes


















1















  1. Consider using or instead of and

  2. 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!")





share|improve this answer





















  • 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











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
});


}
});














draft saved

draft discarded


















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









1















  1. Consider using or instead of and

  2. 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!")





share|improve this answer





















  • 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
















1















  1. Consider using or instead of and

  2. 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!")





share|improve this answer





















  • 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














1












1








1







  1. Consider using or instead of and

  2. 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!")





share|improve this answer













  1. Consider using or instead of and

  2. 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!")






share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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


















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%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





















































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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python