2017-06-04 13 views
-3

私はPythonを初めて使うので、たくさんのことは分かりません。しかし今、私はdefine関数について知る必要があります。 私は自分のプログラム(スクリプト)を繰り返そうとしていましたが、私は 'def'ステートメントを使用できることを学びました。だから私はdef start():をプログラム(スクリプト)を繰り返す必要があるところに置いたが、その後は何も動作しなかった。`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

あなたの問題を説明するためにgazillionのif文は必要ありません。できるだけ多くを削除して、問題を説明できるようにしてください。 –

答えて

0

インデントを忘れたようです。

def start(): 
    word=random.choice(words) 
    length=len(word) 
    if word=='hello': 
     # etc. 
関連する問題