如何使用`def`来定义函数?

时间:2017-06-04 16:13:37

标签: python loops repeat

我是Python的新手,所以我不了解很多东西。但是现在,我需要了解define函数。 我试图重复我的程序(脚本),我了解到我可以使用' def'声明。所以我把'def start():'我需要重复程序(脚本)的地方,但是,之后的任何事情都没有完成

这是我正在进行的计划。

def start():
word=random.choice(words)
length=len(word)
if word=='hello':
    print('This is a word that you say when you greet someone')
elif word=='car':
    print('This is a machine that you ride to go to a place to another')
elif word=='run':
    print('This is to go faster than walking')
elif word=='apple':
    print('This is a round and red fruit that grows on the tree')
elif word=='day':
    print('This is the time of light between one night until the next night')
elif word=='month':
    print('This is a measure of time. Each 28~31 days is one of this')
elif word=='cat':
    print('This is an carnivore animal that is also treated as a pet from home')
elif word=='dog':
    print('This is an animal that is related to wolves, foxes, and gray wolves')
elif word=='water':
    print('This is a liquid that people needs to drink to survive')
elif word=='candy':
    print('This is a crystallized form of sugar')
elif word=='snack':
    print('This is a light meal that you eat between regular meals')
elif word=='food':
    print('This is a obect that people needs to eat to survive')
elif word=='smell':
    print('This is a sense that you feel by your nose')
elif word=='fast':
    print('This is to do something quickly than usual')
elif word=='hard':
    print('This is something to not be easily broken')
elif word=='cool':
    print('This is something you feel when you little cold')
elif word=='safe':
    print('This is to be away from harm')
elif word=='hurt':
    print('This is to have pain')
elif word=='time':
    print('This is a period when an event happens')
elif word=='glass':
    print("This is an object that doesn't absorb a lot of light so you can see through it")
else:
    print('This is a animal that can fly above the sky')
while life > 0:
    print('The length of the word is {0} letters'.format(length))
    fl=input('Guess the first letter of the word! :')
    if fl==word[0]:
        print('Whoah! Nice guess {0}'.format(pn))
        sl=input('Now guess the second letter of the word! :')
        score+=1
        if sl==word[1]:
            print('Great job! You got it {0}'.format(pn))
            if len(word)==3:
                tl=input('Now you need to guess the last letter! :')
                if tl==word[2]:
                    print('You got some skills on guessing {0}!'.format(pn))
                    finalWordThird=input('Now write the word that you guessed! :')
                    score+=1
                    if finalWordThird==word:
                        print('You got the word! Congratulations!')
                        score+=5
                        start()
                    else:
                        life-=1
                        print('I guess you typed the word wrong! You have {0} lives left!'.format(life))
                else:
                    life-=1
                    print('Nice guess but wrong. Try again! You have {0} lives left!'.format(life))          
            else:
                ttl=input('Now guess the third letter! :')
                if ttl==word[2]:
                    print('You got some skills on guessing {0}!'.format(pn))
                    score+=1
                    if len(word)==4:
                        ql=input('Now you need to guess the last letter! : ')
                        if ql==word[3]:
                            print('You guessed the last letter right {0}!'.format(pn))
                            finalWordFourth=input('Now write the word you guessed! : ')
                            score+=1
                            if finalWordFourth==word:
                                print('You guessed the word right! Congratulations!')
                                score+=6
                                start()
                            else:
                                 life-=1
                                 print('I guess you typed the word wrong! You have {0} lives left!'.format(life))
                        else:
                            life-=1
                            print('Nice guess but wrong. Try again! You have {0} lives left!'.format(life))
                    else:
                        qql=input('Now guess the fourth letter! : ')
                        if qql==word[3]:
                            print('You are an incredible guesser {0}!'.format(pn))
                            score+=1
                            if len(word)==5:
                                cl=input('Now guess the last fifth letter! : ')
                                if cl==word[4]:
                                    print('You guessed your final letter right {0}!'.format(pn))
                                    finalWordFifth=input('Now write the word that you guessed! : ')
                                    score+=1
                                    if finalWordFifth==word:
                                        print('You got the word! Congratulations!')
                                        score+=7
                                        start()
                                    else:
                                        life-=1
                                        print('I guess you typed the word wrong! You have {0} lives left!'.format(life))
                                else:
                                    life-=1
                                    print('Nice guess but wrong. Try again! You have {0} lives left!'.format(life))
                        else:
                            life-=1
                            print('Nice guess but wrong. Try again! You have {0} lives left!'.format(life))
                else:
                    life-=1
                    print('I guess you typed the word wrong! You have {0} lives left!'.format(life))
        else:
            life-=1
            print('Nice guess but wrong. Try again! You have {0} lives left!'.format(life))
    else:
        life-=1
        print('Nice guess but wrong. Try again! You have {0} lives left!'.format(life))

print('Your score is {0}! Great job {1}!'.format(score,pn))
restart=input('Will you play again? (Y/N): ')
if restart == "Y" or "y":
    start()
else:
    quit()

对长程序感到抱歉。

1 个答案:

答案 0 :(得分:0)

看起来你忘记了缩进。

def start():
    word=random.choice(words)
    length=len(word)
    if word=='hello':
        # etc.