Skip to main content
 首页 » 编程设计

Python 是/否用户输入

2025年05月04日29jpfss

我正在尝试创建一个用户输入来写入当前代码有效的文件,但我必须在它们周围写下我的答案,无论如何我可以只写 yes 或 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") 

请您参考如下方法:

改用raw_input:

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

raw_input 获取字符串形式的输入内容,而 input 获取确切的用户输入并将其作为 Python 进行评估。您必须输入"is"而不是"is"的原因是因为您需要将输入评估为字符串。 raw_input 意味着您不需要这样做。

Python 3.x 注释

raw_input 在 Python 3.x 中更改为 input。如果您需要 input 的旧功能,请改用 eval(input())。