Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python是/否用户输入_Python_Python 2.7_Input - Fatal编程技术网

Python是/否用户输入

Python是/否用户输入,python,python-2.7,input,Python,Python 2.7,Input,我正在尝试创建一个用户输入,用于写入文件当前代码正常工作,但我必须用“”来写我的答案。无论如何,我可以只写“是”或“Y”,而不必包含“” Join = input('Would you like to write to text file?\n') if Join in ['yes', 'Yes']: for key, value in ip_attacks.iteritems(): #for key(the IPAddress) and value(the occurrence of

我正在尝试创建一个用户输入,用于写入文件当前代码正常工作,但我必须用“”来写我的答案。无论如何,我可以只写“是”或“Y”,而不必包含“”

Join = input('Would you like to write to text file?\n')
if Join in ['yes', 'Yes']:
    for key, value in ip_attacks.iteritems(): #for key(the IPAddress) and value(the occurrence of each IPAddress) in ip_attacks 
        if value > 30: #if the value (number of occurrences) is over 30  
            myTxtFile.write('\n{}\n'.format(key)) #then we want to write the key(the IPAdress) which has been attack more than 30 times to the text file
else:
    print ("No Answer Given")

使用
原始输入

Join = raw_input('Would you like to write to text file?\n')
raw_input
获取作为字符串的输入,而
input
获取准确的用户输入,并将其作为Python进行计算。您必须输入“是”而不是“是”的原因是您需要将输入值作为字符串进行求值<代码>原始输入表示您不需要这样做

Python3.x的注释
Python3.x中的
raw_input
已更改为
input
。如果您需要
input
的旧功能,请改用
eval(input())

改用
raw\u input
。看

因此,在您的第一行中,您将使用:

Join = raw_input('Would you like to write to text file?\n')

不要在Python2.x中使用
input
;使用
原始输入
input
相当于
eval(原始输入(…)
,这意味着您必须键入一个字符串以形成有效的Python表达式


在Python3中,
raw_input
被重命名为
input
,而前者
input
被从语言中删除。(您很少希望将输入作为表达式进行求值;当您这样做时,您可以自己调用
eval(input(…)

您可以将if语句更改为使用lower()或upper()与字符串进行比较,而不必在“yes”或“y”周围使用单引号,如下所示

if Join.lower() == 'yes' or Join.lower() == 'y':
如果使用Python3,请尝试以下操作:

Join = input('Would you like to write to text file?\n')
if Join.lower() == 'yes' or Join.lower() == 'y':
    for key, value in ip_attacks.iteritems(): #for key(the IPAddress) and value(the occurrence of each IPAddress) in ip_attacks
        if value > 30: #if the value (number of occurrences) is over 30
            myTxtFile.write('\n{}\n'.format(key)) #then we want to write the key(the IPAdress) which has been attack more than 30 times to the text file
else:
    print ("No Answer Given")
否则,如果使用Python2,正如其他人所说,您将希望使用raw_input()而不是input()