2017-01-28 9 views
-3

私は自分のプログラム(コード)を複製せずに繰り返す方法を見つけようとしましたが、私が読んだ答えはすべて意味がありませんでした。誰か助けてくれますか? これは私が永遠の真の条件がありますwhileループを行います私のプログラムを複製せずに繰り返す方法

import random 
words=['hello','run','apple','day','month','cat','dog','bird','car','water'] 
word=random.choice(words) 
length=len(word) 
life=50 
print('\t\tGuess the word!') 
print('instructions: Guess the word. The word is only written by the alphabets.') 
pn=input('Type your player name :') 
print('Use this to help you! :',words) 
print('The length of the word is',length,'letters') 
fl=input('Guess the first letter of the word! :') 
if fl==word[0]: 
    print('Whoah! Nice guess',pn) 
else: 
    life=life-1 
    print('Nice guess but wrong. Try again! You have',life,'lives left!') 
+4

を聞いたことがありますか? – miindlek

+0

これらの回答のうち、どの部分が意味をなさないのですか?あなたはおそらく同じ回答を受け取るでしょう – Sayse

答えて

0

に取り組んでいたプログラムです

だけながら、(真の)追加:のために実行するために、そのスニペットを作成するコードにをこれまで...

import random 

while(True): 
    words=['hello','run','apple','day','month','cat','dog','bird','car','water'] 
    word=random.choice (words) 
    length=len(word) 
    life=50 
    print('\t\tGuess the word!') 
    print('instructions: Guess the word. The word is only written by the alphabets.') 
    pn=input('Type your player name :') 
    print('Use this to help you! :',words) 
    print('The length of the word is',length,'letters') 
    fl=input('Guess the first letter of the word! :') 
    if fl==word[0]: 
     print('Whoah! Nice guess',pn) 
    else: 
     life=life-1 
     print('Nice guess but wrong. Try again! You have',life,'lives left!') 
0

は、私はそれを正しく理解すればわからないんだけど、あなたはおそらく正しいかどうかブール値を追加し、正しい答えを与えたまでループし続けるために、whileループを使用することができます。

while incorrect: 
    run 
    loop 
answer is correct 

ループ内のコードをインデントする必要がありますので、多分同様 何か。

0

私は、これはあなたがやろうとしているものであると信じて:あなたは今までのループについて

import random 

life=50 
words=['hello','run','apple','day','month','cat','dog','bird','car','water'] 
print('\t\tGuess the word!') 
print('instructions: Guess the word. The word is only written by the alphabets.') 
pn=input('Type your player name :') 
print('Use this to help you! : {0}'.format(words)) 

while life: 
    word=random.choice(words) 
    length=len(word) 
    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)) 
    else: 
     life=life-1 
     print('Nice guess but wrong. Try again! You have {0} lives left!'.format(life)) 
関連する問題