0

I use Python 3.7.1.

I will introduce my expectations to be clearer for the reader. I enter a choice through the input() and if I wrote a while loop, this is in order to ask me again if I enter a wrong "command"(a wrong input).

So let's see the script.py below:

champ_choix=""
while champ_choix!="1" or champ_choix!="2":
    champ_choix=input("Pour cat_course tapez 1\nPour hippodrome tapez 2\n")
    print("L'input est : {}".format(champ_choix))#print the input is:
    print("le type de l'input est: {}".format(type(champ_choix)))#print the type is:

    if champ_choix=="1":
    ##CHOIX DE LA COLLECTION##
        collection_num=""
        while collection_num!="1" or collection_num!="2":
            collection_num=input("Pour la collection geny_rapp tapez 1\nPour la collection geny_cotes tapez 2\n")
        #Do some instructions useless to show

    if champ_choix=="2":
        subprocess.run(['scrapy crawl test_shell -a nom_prix=True'],shell=True)
        #if choice is "2" launch a spider with scrapy

I need to precise that I tried first without indented the if conditions above, and using other words of conditions: if champ_choix=="1": .... elif champ_choix=="2":... else: pass but it still does not work.

The issue is when I launch the script in the terminal it gives me:

(base) avy@avy-Moi:~/folder$ python script.py
Pour cat_course tapez 1
Pour hippodrome tapez 2
2 <- the input I entered
L'input est : 2
le type de l'input est: <class 'str'>
Pour cat_course tapez 1
Pour hippodrome tapez 2

As you can see, it really instantiate the right input: 2, and it is the right type of the input: str. But it makes an infinite loop, and don't understand why because considering this tutorial that's the same way.

2 Answers 2

3
while champ_choix!="1" or champ_choix!="2"

doesn't work like you think it does. champ_choix would have to have two values at the same time for that to ever be false, and that isn't possible.

You want to make sure it isn't equal to "1", and it isn't equal to "2":

while champ_choix!="1" and champ_choix!="2"
Sign up to request clarification or add additional context in comments.

2 Comments

I changed as you said, replace the or by and but I still have the infinite loop.
Yes, you're right, I will accept your answer as soon as possible. I wrote a print() within the choice equal 2. It works, that's a matter of subprocess now because it does nothing as I expected first of all, and I did not expect it asks me again when instructions are done. Thank you.
2

The easiest way to check champ_choix against 1 or 2 is using in, or in this case not in:

while champ_choix not in ('1', '2'):

Equally, you could use a not around the whole expression and negate the comparison operators (turn != into ==):

while not (champ_choix == '1' or champ_choix == '2'):

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.