Python如何只接受用户输入的某些单词

时间:2015-07-25 11:04:23

标签: python list input xbmc kodi

好的,脚本现在正在运行,我想感谢大家的建议。

继承人最后的剧本

import smtplib 
import xbmc
import xbmcgui
import datetime

list = ("mary", "james", "tilly")

kb = xbmc.Keyboard('', 'Please type in your name to continue')
kb.doModal()
typedin = kb.getText()

if typedin.lower() in list:
    now = datetime.datetime.now()
    runtime = now.strftime("%Y-%m-%d %H:%M")

    content = xbmc.executebuiltin('kb.getText()')
    mailserver = smtplib.SMTP("smtp.mail.com",25)
    mailserver.ehlo()
    mailserver.starttls()
    mailserver.login('mail@somemail.com','somepwd')
    mailserver.sendmail('mail@somemail.com','mail@somemail.com',typedin + ' has run proggy ' + runtime)
    mailserver.close()
    xbmc.executebuiltin("Notification(An email has been sent, yada yada yada,()")
else:
    xbmc.executebuiltin("THE INPUTTED NAME, IS NOT VALID,()")
    xbmcgui.Dialog().ok(
    "Please try again - User name not correct",
    "The input yada yada",
    "yada yada",
    "yada yada")

所以,只是为了让你知道即时通讯使用可在25端口上运行的实时邮件。 在windows和linux以及openelec上试过并测试过。工作正常。

3 个答案:

答案 0 :(得分:2)

也许是if语句的内容?因此,只有给出其中一个词,然后做其余的,否则不接受。

mytool.exe

答案 1 :(得分:1)

在希望从用户获取输入的情况下,最好的做法是使用raw_input语句执行while循环,如:

word_list = ['simon', 'tom', 'sarah', 'peter', 'jane', 'sue']

def get_word():
    """returns the word that the user chooses"""
    print("Please choose a name from the following:")
    w_string = ""
    for w in word_list:
        w_string += w + ', '
    #print the word list without a comma at the end.
    print(w_string[:-1])

    while True:
        word = raw_input("\n> ") #use input("\n> ") for python3
        #If you wish to accept something like "SiMoN      " or "S Imon "
        #have the following line:
        word = word.lower().strip()
        if word in word_list:
            return word
        else:
            print("'%s' is not in the list of words. Please choose from the following:\n%s" % (word, w_string[:-1]))

word = get_word()
print(word)

答案 2 :(得分:0)

我最好的选择是不要将列表转换为字符串并检查输入的名称是否在列表中,如...

import smtplib
import xbmc
import xbmcgui
import datetime

nameList = ["simon", "tom", "sarah", "peter", "jane"]
kb = raw_input('Please type in your name to continue:')

if kb in nameList:
    now = datetime.datetime.now()
    runtime = now.strftime("%Y-%m-%d %H:%M")

    content = xbmc.executebuiltin('kb.getText()')
    mailserver = smtplib.SMTP("smtp.mail.com",25)
    mailserver.ehlo()
    mailserver.starttls()
    mailserver.login('mail@somemail.com','somepwd')
    mailserver.sendmail('mail@somemail.com','mail@somemail.com',typedin + 'has run keymail ' + runtime)
    mailserver.close()
else:
    print ("The Name %s, was not in the list")%kb

并且不要使用list和str作为变量名。