Python是/否用户输入 [英] Python Yes/No User Input

查看:85
本文介绍了Python是/否用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建用于将当前代码写入文件的用户输入,但是我必须在它们周围写上"的答案,无论如何我都可以只写是"或"Y"而不必包含"

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 ' '

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")

推荐答案

请改用 raw_input :

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

raw_input 获取作为字符串的输入,而 input 获取确切的用户输入并将其评估为Python.之所以必须输入"Yes"而不是"Yes",是因为您需要将输入求值字符串作为一个字符串. raw_input 意味着您不需要这样做.

raw_input gets what the input was as a string, while input gets the exact user input and evaluates it as Python. The reason you're having to put "Yes" rather than Yes is because you need to make the input evalute as a string. raw_input means that you don't need to do that.

raw_input 已更改为 input .如果需要 input 的旧功能,请改用 eval(input()).

raw_input was changed to input in Python 3.x. If you need the old functionality of input, use eval(input()) instead.

这篇关于Python是/否用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆