代码之家  ›  专栏  ›  技术社区  ›  jos

我在想办法让python简单地发布一首歌的歌词

  •  0
  • jos  · 技术社区  · 17 年前

    我是python编程的初学者,在我的一个作业中,我将使用python编写一个代码来表达这些歌词。

    这个老人,他玩了一个
    他在我的拇指上耍小把戏

    这个老人滚回家来了

    这位老人,他扮演了两个角色
    他在我的鞋子上耍小把戏
    小摆设,给你的狗一根骨头
    这个老人滚回家来了

    老兄,他玩了这个
    他在我膝盖上耍小把戏
    小摆设,给你的狗一根骨头
    这个老人滚回家来了

    这位老人,他玩了四个
    他在我的门上耍小把戏
    小摆设,给你的狗一根骨头
    这个老人滚回家来了


    他在我的蜂箱上耍小把戏
    小摆设,给你的狗一根骨头


    他在我的棍子上耍小把戏

    这个老人滚回家来了

    这位老人,他玩了七
    他在天上耍小把戏
    小摆设,给你的狗一根骨头
    这个老人滚回家来了


    他在我的大门上耍小把戏
    小摆设,给你的狗一根骨头
    这个老人滚回家来了



    小摆设,给你的狗一根骨头

    这位老人,他打了十
    他又玩了一次小把戏
    小摆设,给你的狗一根骨头

    这是我目前所拥有的,但它并没有实现我想要它做的事情。我不知道如何使用while循环,或者让它按顺序从列表中选择一个单词。

    num = ['one','two','three','four','five','six','nine','ten']
    end = ['on my thumb','on my shoe','on my knee','on my door','on my hive','on my sticks','up in heaven','on my gate','on my spine','once again']
    z=1
    
    print "This old man, he played",(num)
    print "He played knick-knack", (end)
    print "Knick-knack paddywhack, give your dog a bone"
    print "This old man came rolling home"
    
    7 回复  |  直到 17 年前
        1
  •  5
  •   Adam Batkin    17 年前

    提示:您需要使用循环

    Python Flow Control range() 功能。

    你可以抓住 '数组中的第个元素:

    val = some_array[n]
    

    请记住,在Python中,数组从0开始计数,而不是1。

        2
  •  2
  •   Kathy Van Stone    17 年前

    事实上,你的家庭作业有一个更像蟒蛇的答案。再说一次,我不能直接给你,但是除了看看 loop documentation ,您可能还想看看 zip . 在Python中可以做很多事情,而不必直接使用索引变量。

        3
  •  2
  •   steveha    17 年前

    首先,阅读Python中的元组和列表;然后阅读for循环。

    # define a list of tuples
    lst = [ ("eggs", "an omelet"), ("bread", "a sandwich"), ("sugar", "cookies") ]
    
    for ingredient, food in lst:
        print "I need", ingredient, "to make", food + "."
    

    如果运行上述代码,将得到以下输出:

    I need eggs to make an omelet.
    I need bread to make a sandwich.
    I need sugar to make cookies.
    

    这是解决这个问题的蟒蛇式方法。还有一种方法,我也不喜欢:

    ingredients = ["eggs", "bread", "sugar"]
    foods = ["an omelet", "a sandwich", "cookies"]
    
    for i in range(len(ingredients)):
        print "I need", ingredients[i], "to make", foods[i] + "."
    

    Learning Python 研究一下,它会教你很多,而且很清楚。

        4
  •  0
  •   Elias Zamaria    17 年前

    我不确定是否要做你的作业,但我会给你一些提示:对我来说,这看起来是一个for循环的好地方。使用range、xrange或enumerate。

        5
  •  0
  •   Chris Lutz    17 年前

    z = 0 因为Python(和大多数编程语言)中的数组是零索引的- num 是 num[0] ,第二个是 num[1] ,以此类推。虽然我们根本不需要 z . 稍后再谈。

    号码 和 end 到 nums 和 endings . 如果我们给他们一个复数的名字,我们使用的是一个列表就更清楚了。此外,列表不是单个值,而是值的集合。我们需要一个 在这个案子里。我们不会说 (nums) (或 (num) nums[x] num[x] )得到元素 x (记住数组是零索引的!),其中 十

    第三,你可以用 while 循环,但最好是 for 循环和 range() 和 len() 循环是:

    for x in y:
    

    在哪里? 十 y 是项目列表。循环遍历中的所有项 是的 ,将每个设置为 十 对于循环体。这个 范围() 长度()

        6
  •  0
  •   Jimmy    17 年前

    前半部分不知羞耻地抄袭了奈勒的回答:

    # List of rhymes, with each part of the rhyme as another list.
    rhymes = ['one','on my thumb'],['two','on my shoe']
    
    for number, position in rhymes:
        # print your output in terms of number and position
    
        7
  •  0
  •   mikemaccana    17 年前

    你的押韵鼓励对爱尔兰人的暴力。不过,还是有一些帮助。

    因为押韵本身有两个部分,所以我把每一个押韵作为一个两部分的列表,放在一个所有这些押韵的列表中。

    # List of rhymes, with each part of the rhyme as another list.
    rhymes = ['one','on my thumb'],['two','on my shoe']
    
    # The total amount of rhymes len(rhymes) lets us know when to stop...
    count = 0
    while count < len(rhymes):
        print "This old man, he played "+rhymes[count][0]
        print "He played knick-knack "+rhymes[count][1]
        print "Knick-knack paddywhack, give your dog a bone"
        print "This old man came rolling home \n"
        count += 1