-1

I am new to Python. It is my first programming language and I am having a hard time comprehending what people think is simple (it probably is). It's making me feel stupid to say the least. I could very much use some pointers with something I am stuck on. Essentially what I need to do is create a program which will create a text file containing the words of 10 green bottles.

def kevin():
    file = open('greenbottles.txt','a')
    file.write(a)
    file.close()

def skinner():
    kevin('rabbits\n')

skinner()

This is what I've done so far to create and write to a text file. (The file should appear in same directory as the Python file is located according to what I've seen and read. I've noticed sometimes it doesn't appear?) When I open the file it should have rabbits written in it and \n means to force create a new line.

What I need to do is write the verses of the song in the text file using lists and variables. Also most of the song should be created in a loop (I thought a while loop would be best). I would be thankful if someone can give me some pointers on how to add a verse in.

"Ten green bottles
Hanging on the wall
Ten green bottles
Hanging on the wall
And if one green bottle
Should accidentally fall
There'll be nine green bottles
Hanging on the wall"

One of the verses.

What I am expecting is that this code should create a text file containing the words rabbit. The kevin and skinner is what I named the functions so I could understand it a little better; they can be called whatever you would like.

4

5 回答 5

1

所以,

我可以看到您是新人,我可以判断的一种主要方式是您的函数名称都没有任何意义!关于编程可以帮助人们在堆栈溢出方面帮助您的一件主要事情是给您的函数/对象起个好名字。凯文和格林不是有用的名字。

在那个主题上,制作两个 green() [不知道它应该从名称中做什么......也许 open 会是一个更好的名称?或写了?] 函数将不允许您同时使用它们。如果必须,请命名一个 green1 和另一个 green2。

现在,就您的程序而言,我认为这样做的总体思路如下:

  • 有一个名为bottleText(i) 的函数,其中i 是一个整数。BottleText 将经文作为字符串返回。您可能还想导入一个函数,您可以在其中将整数转换为数字或自己编写辅助函数。
  • 创建一个循环调用您的 green() 函数并将文本写入文件。我实际上将函数更改为 green(str) 并使 str 成为要编写的文本。

这有任何意义吗?如果您有任何问题,请随时发表评论。

于 2013-09-12T06:53:11.287 回答
1

这里有一些代码可以揭开问题的基本算法的神秘面纱。它不会完全按照您的要求进行操作,而是会以我选择的某种随机格式打印歌词。要理解这段代码,你必须理解列表、for循环、迭代和输出。现在你的工作就是理解代码并让它做你想做的事情,并输出到一个文件中。如果您在理解代码时遇到任何困难,请询问。

这并不能完全回答您的提示,但由于您的问题基本上只是“如何”循环播放并为歌曲创作诗句,我想用列表、for循环和字符串进行简单的演示。

numbers = [
'Zero',
'One',
'Two',
'Three',
'Four',
'Five',
'Six',
'Seven',
'Eight',
'Nine',
'Ten' ]

text_one = 'green bottles hanging on the wall'
text_two = 'And if one green bottle should accidentally fall\nThere\'ll be'

# Each iteration of this loop prints one verse of the song
for i in range(10, 0, -1):
    print(numbers[i], text_one)
    print(numbers[i], text_one)
    print(text_two, numbers[i-1], text_one)
于 2013-09-12T06:57:23.523 回答
0

首先,Python 对你如何分隔事物很挑剔。在这个例子中,你实际上需要缩进定义函数“green”的代码块。例如:

def green():
    file = open('greenbottles.txt','a')
    file.write(a)
    file.close()

我不确定为什么文件不会出现,除非您忘记关闭文件,或者您对所在目录没有足够的写入权限。现在,代码的第二部分重新定义了功能绿色。如果您想使用绿色功能,则不需要“def”。如果要将变量插入文本,则可以像这样“插入”它们:

def green():
    my_variable = 10
    my_bottle_color = "green"
    my_string = "HI! I have %s %s bottles!" % (my_variable, my_bottle_color)
    file = open('greenbottles.txt','a')
    file.write(my_string)
    file.close()
于 2013-09-12T06:59:31.247 回答
0

我将从阅读http://www.99-bottles-of-beer.net/开始,该站点提供了有关如何创建循环的示例,该循环将解决您的问题,而与您尝试编写的语言无关。(对于任何刚接触特定语言的人来说,这也是一个很好的资源。)特别是对于python ,您可以从他们的任何示例开始,但阅读其中的一些示例以尝试了解解决方案类型的基本性质。祝你好运。

于 2015-03-10T12:55:18.477 回答
0

试试这个,它可以通过添加的功能满足您的需求:

def main():
    import time
    while True:
        try:
            num1 = int(input("Pick a number between 10 and 30: "))
        except ValueError:
            print("That's not a number!")
            continue
        if num1 > 30 or num1 < 10:
            print("Not Valid!")
            continue
       break           
    while True:
        try:
            hue = str(input("Pick a colour; Red, Green, Blue: ")) 
        except ValueError:
            print("Letters not numbers!") 
            continue                        
        if  (hue == "Red") or (hue == "red") or (hue == "Green") or (hue == "green") or (hue == "Blue") or (hue == "blue"):

            break
    print("Generating File")
    time.sleep(3)
    numbers ='no', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen', 'Twenty', 'Twentyone', 'Twentytwo', 'Twentythree', 'Twentyfour', 'Twentyfive', 'Twentysix', 'Twentyseven', 'Twentyeight', 'Twentynine', 'Thirty' 
    text_one = hue +' bottle%s\nHanging on the wall'
    text_two = "And if one " + hue + " bottle\nShould accidentally fall\nThere'll be"
    text_three =' \n'

    with open(numbers[num1] + ' ' + hue + ' Bottles.txt', 'w') as a:
        for l in range(num1, 0, -1):                                              
            a.write(numbers[l]+ ' ')
            if l == 1:
                a.write(text_one % '' +'\n')
            else:
                a.write(text_one % 's' +'\n')

            a.write(numbers[l] + ' ')
            if l == 1:
                a.write(text_one %  '' + '\n') 
            else:
                a.write(text_one % 's' +  '\n')
            a.write(text_two + ' ')
            a.write(numbers[l-1] + ' ')
            if (l - 1) ==1 :
                a.write(text_one % ''+'\n')
            else:
                a.write(text_one % 's'+'\n')
            a.write('\n' + '\n')


if __name__ == '__main__':
    main()
于 2015-06-09T03:37:58.217 回答