0

I use Python 3.7.1 (default, Dec 14 2018, 19:28:38).

First of all my script is running correctly the second time I enter a correct entry in input(), but I prefer it asks only once. In my code I wrote print() lines in order to see what is happening in real time and that's where I don't get what happens in the execution.

Let's see the script:

import subprocess
from scrapy import cmdline
import sys

champ_choix = ""
while champ_choix != "1" and champ_choix != "2":
    champ_choix=input("Pour cat_course tapez 1\nPour hippodrome tapez 2\n")
    print("L'input est : {}".format(champ_choix)) #the input is:
    print("le type de l'input est: {}".format(type(champ_choix))) #the type of input is:
    if champ_choix == "1": #Traite le champ hippodrome
        print("on a le champ_choix 1") #we have got choice 1
        #do instructions
        sys.exit() #to stop the script and stop asking once again an entry after it is done
    if champ_choix == "2":
        print("le champ_choix est 2") #we have got choice 2
        cmdline.execute(['scrapy','crawl','test_shell','-a','nom_prix=True'])
        sys.exit()

Now what I have in the terminal is:

Pour cat_course tapez 1
Pour hippodrome tapez 2
2 # here a good entry
L'input est : 2 # it says it is a good entry
le type de l'input est: <class 'str'> # it even says that's the good type
le champ_choix est 2 # it even says it is in the second `if` condition
Pour cat_course tapez 1 # but it asks once again, without executing instructions
Pour hippodrome tapez 2
2 # a good entry once more
L'input est : 2
le type de l'input est: <class 'str'>
le champ_choix est 2 # in the second `if` condition and now gonna finally execute instructions
#Running the spider as expected, that is alright
#Stop and exit, it is alright too

What is wrong?

1
  • 1
    Use break instead of sys.exit(). Otherwise, when I ran this script, it asked me input only once. Commented May 23, 2019 at 18:39

1 Answer 1

1

Well worded question (props for giving the stdout) but make sure you are doing proper spacing (i.e. there should be a space before and after operators such as a == b).

1) Don't use sys.exit() just use break.

2) You need to prime your loop. In other words do one input before you
enter the loop then keep inputting at the END of your loop:

import subprocess
from scrapy import cmdline
import sys

while True: 
    champ_choix = input("Pour cat_course tapez 1\nPour hippodrome tapez 2\n")

    # This first if isn't needed because the if at the bottom catches this case
    # if champ_choix == "1": #Traite le champ hippodrome
    #     break
    if champ_choix == "2":
        cmdline.execute(['scrapy','crawl','test_shell','-a','nom_prix=True'])
    if champ_choix in ["1", "2"]:
        break
Sign up to request clarification or add additional context in comments.

13 Comments

while champ_choix not in ["1", "2"] :)
I changed as you shown, but after I entered the input it stoped simply and did not execute instructions, it did not print "le champ_choix est 2" as it must.
@AvyWam see my update. What you needed was do while not a simple while and I wan't paying attention.
Ok. I did as you wrote. Changing for while True and put if champ_choix == "2": at first, because in the contrary it doesn't work as expected, and if champ_choix in ["1", "2"]: at last, and I remove the break key word in if champ_choix == "2": too, exactly as you did. The script works, but it asks me two times again. And to be honest, in if champ_choix in ["1", "2"]: there is another while loop and I commented it to see if by "magic" it would be due to that but actually it is not the root of the repetition issue.
@AvyWam Seems to work right. Is there a test case I am missing? (Did you remove the input above the while loop)?
|

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.