2017-06-04 119 views
-3

我是新來的Python,所以我不知道很多事情。但是現在,我需要了解定義函數。 我試圖重複我的程序(腳本),我知道我可以使用'def'語句。所以我把「高清啓動():」在這裏我需要重複的程序(腳本),但隨後,無論是後一點都沒有如何使用`def`來定義一個函數?

這項工作是我工作的程序。

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() 

很抱歉,很長的程序。

+2

嘗試一個最小的例子(參見[mcve]),並且包括什麼「但是,然後,什麼是它沒有工作之後」的意思。錯誤?例外?奇怪的結果?錯誤的結果?沒有結果? – MSeifert

+1

在def start()之後你是否正確地縮進了你的代碼:'???爲此,使用字典更好。 – nacho

+1

你不需要一個巨大的if語句來說明你的問題。請儘可能多地刪除,同時仍然能夠說明您的問題。 –

回答

0

看起來你忘了壓痕。

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