简体   繁体   English

Python是/否用户输入

[英]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意味着您不需要这样做。

Note for Python 3.x Python 3.x的注意事项

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(...)) 。)

Use raw_input instead. 请改用raw_input See docs . 参见docs

So in your first line you would use: 因此,在第一行中,您将使用:

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

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.

相关问题 在python中自动给用户输入yes / no - Automatically give user input yes/no in python Python 2.7 Argparse是或否输入 - Python 2.7 Argparse Yes or No input 有/无输入的While循环(Python) - While Loop With Yes/No Input (Python) python会提示用户作为Visual Studio Console应用程序输入信息吗? 如果是,请举一个例子 - Will python prompt for input from user as a Visual Studio Console application does? if yes please give an example 检查 python MySQL 中是否存在记录,如果未找到记录,则让用户根据选择是/否添加该记录(通过用户输入)? - Check if record exits in python MySQL and if the record not found then let the user to add that record (via user input) based on choice YES/NO? 如何用是或否问题限制用户输入 python - How to restrict useer input with a yes or no question python 如果输入为“是”,为什么Python突然停止? - Why does Python suddenly stop if the input is Yes? Python CLI-对所有用户问题回答是 - Python CLI - Answer yes to all user questions 询问用户输入,如果是执行功能,如果没有退出程序 - Ask for user input, if yes do function, if no quit program Python Fabric如何发送密码,是/否发送给用户提示 - Python fabric How to send password and yes/no for user prompt
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM