[英]Python Yes/No User Input
I am trying to create a user input for writing to a file the current code works but i have to write my answer with ' ' around them is there anyway i can just write yes or Y without having to include ' ' 我正在尝试创建用于将当前代码写入文件的用户输入,但是我必须在它们周围写上带有“”的答案,无论如何我都可以只写“是”或“ 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")
Use raw_input
instead: 使用
raw_input
代替:
Join = raw_input('Would you like to write to text file?\n')
raw_input
gets what the input was as a string, while input
gets the exact user input and evaluates it as Python. raw_input
获取作为字符串的输入内容,而input
获取确切的用户输入并将其评估为Python。 The reason you're having to put "Yes" rather than Yes is because you need to make the input evalute as a string. 之所以必须输入“ Yes”而不是“ Yes”,是因为您需要将输入求值字符串作为字符串。
raw_input
means that you don't need to do that. raw_input
意味着您不需要这样做。
raw_input
was changed to input
in Python 3.x. raw_input
已更改为Python 3.x中的input
。 If you need the old functionality of input
, use eval(input())
instead. 如果您需要
input
的旧功能,请改用eval(input())
。
Don't use input
in Python 2.x; 不要在Python 2.x中使用
input
; use raw_input
. 使用
raw_input
。 input
is equivalent to eval(raw_input(...))
, which means you have to type a string which forms a valid Python expression. input
等效于eval(raw_input(...))
,这意味着您必须输入构成有效Python表达式的字符串。
In Python 3, raw_input
was renamed input
, and the former input
was removed from the language. 在Python 3中,
raw_input
被重命名为input
,并且先前的input
已从语言中删除。 (You rarely want to evaluate the input as an expression; when you do, you can just call eval(input(...))
yourself.) (您很少想将输入作为表达式求值;这样做时,您可以自己调用
eval(input(...))
。)
You can change your if statement to use lower() or upper() to compare with the string and not have to use the single quotes around the 'yes' or 'y' as indicated below 您可以将if语句更改为使用Lower()或upper()与字符串进行比较,而不必在“ yes”或“ y”周围使用单引号,如下所示
if Join.lower() == 'yes' or Join.lower() == 'y':
If using Python3, try this: 如果使用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")
Otherwise if using Python2, as others have stated you would want to use raw_input() instead of just input() 否则,如其他人所述,如果使用Python2,您将要使用raw_input()而不是仅input()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.