如何根据用户编号创建多个变量?

时间:2014-05-12 11:30:11

标签: python variables python-2.7

我目前正在尝试制作一个简单的Pig Latin生成器,它允许句子和单个单词。基本上我需要能够创建一定数量的变量,具体取决于用户为我编写的单词数量,以便能够变成Pig Latin。

我已经得到了我的分词器,它将所有单词存储到一个变量中,之后我用word1 = sentence_list [0]抓住了第一个单词。

TL; DR :如何创建一定数量的变量取决于用户在句子中提交的单词数量?

到目前为止

代码:

print "Welcome to Pig Latin translator! Use this to translate to Pig Latin!"

def start():
    pyg = 'ay'

    original = raw_input("Type a word or sentence here to translate to Pig Latin: ")

    if len(original) > 0: #making sure user doesn't submit nothing
        print "Your word/sentence you want translated is " + original
        word = original.lower() #makes the word/sentence lowercase
        sentence_list = word.split(" ") #splits the sentence into multiple words
        print sentence_list
        lensentence = len(sentence_list) #gets how many different words there are
        word1 = sentence_list[0] #gets the first word in a sentence
        word1 = word + first + pyg #begins translating to pig latin
        word1 = new_word[1:len(new_word)] #finishes transslation by removing first     letter
    else:
        print "You didn't type anything!" #says if user didn't submit any characters

start()

1 个答案:

答案 0 :(得分:0)

希望这可以解决您的问题。这使用列表理解,所以我建议你完成Codeacademy课程,以便很好地掌握它们。如果您有任何问题,可以问一下。

print "Welcome to Pig Latin translator! Use this to translate to Pig Latin!"


def start():
    pyg = 'ay'

    original = raw_input("Type a word or sentence here to translate to Pig Latin: ")

    if len(original) > 0:
        print "Your word/sentence you want translated is " + original
        word = original.lower()
        sentence_list = word.split(" ")
        print sentence_list
        words = [w[1:] + w[0] + pyg for w in sentence_list]
        print " ".join(words)
    else:
        print "You didn't type anything!"

start()