Python如何只接受用户输入的特定单词

Python如何只接受用户输入的特定单词,python,list,input,xbmc,kodi,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 li

好了,现在脚本开始工作了,我要感谢大家的建议

这是最后的剧本

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端口工作的live mail。
在windows、linux和openelec上进行了尝试和测试。工作正常。

可能与if语句类似?因此,只有给出其中一个词,然后再做其余的,否则不要接受

list = ["simon", "tom", "sarah", "peter", "jane"]
word = input()
if word.lower() in list:
    print('Ok')
    #continue with rest of program
else:
     print('No, sorry')

在希望从用户获取输入的情况下,最好使用原始输入语句执行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)

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

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作为变量名。

我很快会尝试一下,然后发回。谢谢Firefly我已经编辑了显示进度的脚本。现在有50%在工作。更多信息在第一篇文章的底部。感谢您的帮助xbmc.executebuiltin(“您使用的名称,不在列表中,()”看起来不正确。我不知道您为什么不使用print,但我认为这应该是:xbmc.executebuiltin(“您使用的名称,不在列表中”)粘贴了最后的代码。感谢所有人的建议。解决方案当用户输入不在列表中时,您希望发生什么情况?目前只需使用else退出:xbmc.executebuiltin(“通知(名称不在列表中,正在退出()”)xbmc.executebuiltin(“激活域(10000,返回)”)xbmc.executebuiltin(“您使用的名称不在列表中,()”已在代码底部,但如果名称不匹配,则不会运行。我没有安装这些东西,因此无法对其进行测试,但是如果在str1:”中使用“if tpyedin.lower()”而不是“if tpyedin.lower()”如何