在python脚本中循环询问两次input()项
我使用Python 3.7.1(默认值,2018年12月14日,19:28:38) 首先,当我第二次在在python脚本中循环询问两次input()项,python,python-3.x,while-loop,Python,Python 3.x,While Loop,我使用Python 3.7.1(默认值,2018年12月14日,19:28:38) 首先,当我第二次在input()中输入正确的条目时,我的脚本运行正常,但我希望它只询问一次。 在我的代码中,我写了print()行,以便实时查看正在发生的事情,而这正是我无法获取执行过程中发生的事情的地方 让我们看看脚本: import subprocess from scrapy import cmdline import sys champ_choix = "" while champ_choix != "
input()
中输入正确的条目时,我的脚本运行正常,但我希望它只询问一次。
在我的代码中,我写了print()
行,以便实时查看正在发生的事情,而这正是我无法获取执行过程中发生的事情的地方
让我们看看脚本:
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()
现在我在航站楼里看到的是:
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
倒入第1类课程磁带
倒马拉松2
2#这是一个很好的入口
L'input est:2#它说这是一个很好的条目
le type de l'input est:#它甚至说那是好的类型
le champ_choix est 2甚至说它处于第二种“如果”状态
倒上1号菜谱,但它又问了一次,没有执行指令
倒马拉松2
2#又是一个不错的入口
美国东部时间:2
输入est类型:
le champ_choix est 2处于第二个'if'状态,现在将最终执行指令
#按预期运行spider,没关系
#停下来退出,也没关系
有什么问题吗?措辞得体的问题(给出标准输出的道具),但要确保你使用了适当的间距(即在操作符前后应该有空格,例如a==b
)
1) 不要使用sys.exit()
只要使用break
2) 你需要给你的循环充注能量。换言之,在输入之前先输入一次输入循环,然后在循环结束时继续输入:
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
使用
break
而不是sys.exit()
。否则,当我运行此脚本时,它只要求我输入一次。虽然champ_choix不在[“1”,“2”]:)中,我按您所示进行了更改,但在输入后,它只是停止并没有执行指令,它没有按必须的方式打印“le champ_choix est 2”。@AvyWam看到我的更新。你需要的是while
而不是一个简单的while
,我不想注意。我照你写的做了。更改为而为True
并首先放置如果champ_choix==“2”:
,因为与预期相反,它不能正常工作,并且如果champ_choix在[“1”,“2”]:
最后,我删除如果champ_choix==“2”:
中的关键字,与您所做的完全一样。剧本很有效,但它又问了我两次。老实说,在<代码>如果champ_choix在[“1”,“2”]:
中有另一个while循环,我对它进行了评论,看看是否是“魔术”造成的,但实际上这不是重复问题的根源。@AvyWam似乎是。我是否缺少一个测试用例?(您是否删除了循环期间上方的输入?