Jump to content

Hi,

just stuck with this piece of code which never stops even if it gets a listed answer,

my aim is for it to repeat until it gets one of the desired answers

 

Code:

 

print("Hello and Welcome to the future")
animal = input("Which is better cats or dogs?")
if animal == "dogs":
    print ("you're wrong")
elif animal == "cats":
    print ("I AGREE")
else:
    input ("What is that?,Which is better cats or dogs?")
while animal != "cats" or "dogs":
    animal == None
    input ("What is that?,Which is better cats or dogs?"),

 

many thanks 

Link to comment
https://linustechtips.com/topic/653685-python-idle-help-while-loop-not-stopping/
Share on other sites

Link to post
Share on other sites

Your loop condition is wrong, you have to do

animal != "cats" and animal != "dogs"

, if animal is not "cats" and animal is not "dogs" it will loop.

 

Problem with

animal != "cats" or "dogs"

is, even if animal != "cats" is false, "dogs" evaluates to true, as it is not compared against string. And or operator is not for joining possibilities or something. Anyway such condition evaluates always to true.

Link to post
Share on other sites

im still confused as when edited this still doesn't work

 

print("Hello and Welcome to the future")
animal = input("Which is better cats or dogs?")
if animal == "dogs":
    print ("you're wrong")
elif animal == "cats":
    print ("I AGREE")
else:
    input ("What is that?,Which is better cats or dogs?")
while animal != "cats" and animal != "dogs":
    input ("What is that?,Which is better cats or dogs?"),
print ("now")

 

still doesn't produce an answer

Link to post
Share on other sites

You're ignoring your input both in else and in while body.

assign it as your first input to animal input

 

But when you first omit to type right answer, the second try (which is in else) or the last tries (in while loop) has no answer handling, so you need to work on it.

The're both should be like:

animal = input ("What is that?,Which is better cats or dogs?")

 

I wont tell you what to do now, try to make it yourself so there will not be unnecessary code duplications. Make sure to post your result here and I will finally correct it if there will be anything wrong with it.

 

What you need to know is that you need one loop for getting answer, and one if for answering to the answer.

Link to post
Share on other sites

Your loop is ask if it not cats and not dogs you need to use or

 

while animal!='cats' or animal!='dogs'

 

you could have done something like this

 

Quote

while True:
    animal = input('which are better cats or dogs')
    if animal == 'cats':
        print('i agree')
        break
    elif animal == 'dogs':
        print('you are wrong')
#this is out side of loop after cats has been picked
print('you were right {} are the best'.format(animals))

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

  • 2 weeks later...

Hi, I think this is the code you wanted:

print("Hello and Welcome to the future")
animal = input("Which is better cats or dogs?")
if animal == "dogs":
    print ("you're wrong")
elif animal == "cats":
    print ("I AGREE")
else:
    while animal != "cats" and animal != "dogs":
       animal =  input ("What is that?,Which is better cats or dogs?")
    print ("now")

The while loop continued because you aren´t referencing the input to "animal".

Edited by Jorge Reus
Link to post
Share on other sites

1 hour ago, Jorge Reus said:

Hi, I think this is the code you wanted:


print("Hello and Welcome to the future")
animal = input("Which is better cats or dogs?")
if animal == "dogs":
    print ("you're wrong")
elif animal == "cats":
    print ("I AGREE")
else:
    while animal != "cats" and animal != "dogs":
       animal =  input ("What is that?,Which is better cats or dogs?")
    print ("now")

The while loop continued because you aren´t referencing the input to "animal".

problem with this is that you're asking for dog or cat then if they don't answer either it then loops to ask for dog or cat.

 

why not skip the first part like I did and go right into the loop?

 

simpler code with the same effect.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

Yes, you´re completely right, but the question of @leonardow9 was specific for his code, any suggestion of another snippet, is completely right, but no necessary.

Despise of this, I agree that a simpler, more readable code is always better.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×