2017-03-01 6 views
0

こんにちは私は完全にプログラミングに新しく、自分自身にパイソンを教えようとしていましたが、単語を選択して文字をシャッフルし、ユーザに推測を入力するように促すプログラムを作成しようとしています3回試みる。私がいる問題は、間違った答えは、それが選択した単語の文字をreshuffles入力があるか、完全に異なる言葉を返したときに、ここで、である私のコードです:ランダムワードゲームpython 3.5

import random 
import sys 

##Welcome message 
print ("""\tWelcome to the scrambler, 
    select [E]asy, [M]edium or [H]ard 
    and you have to guess the word""") 

##Select difficulty 
difficulty = input("> ") 
difficulty = difficulty.upper() 

##For counting number of guesses it takes 
tries = 0 

while tries < 3: 
    tries += 1 

##Starting the game on easy 
if difficulty == 'E': 
    words = ['teeth', 'heart', 'police', 'select', 'monkey'] 
    chosen = random.choice(words) 
    letters = list(chosen) 
    random.shuffle(letters) 
    scrambled = ''.join(letters) 
    print (scrambled) 

    guess = input("> ") 

    if guess == chosen: 
     print ("Congratulations!") 
     break 
    else: 
     print ("you suck") 

else: 
    print("no good") 
    sys.exit(0) 

あなたが見ることができるように、私が唯一として得ています私はそれを少しずつやろうとしていて、他の問題を克服することができましたが、私が持っている問題を解決することはできません。私が抱えている問題や私のコードで気づくかもしれないその他の問題については、どんな助力もありがたいです。

+0

あなたの言葉/スクランブル_before_リトライループを選択してください... –

+0

whileループによってピックアップされるようにifブロックをインデントしたい場合があります。 – Ohjeah

+0

@Ohjeah私はそれを考えましたが、バグの説明ではインデント投稿時のエラー。 –

答えて

1

あなたのゲームにはいくつか改善点と修正点があります。

import random 
import sys 

# Game configuration 
max_tries = 3 

# Global vars 
tries_left = max_tries 

# Welcome message 
print("""\tWelcome to the scrambler, 
select [E]asy, [M]edium or [H]ard 
and you have to guess the word""") 


# Select difficulty 
difficulty = input("> ") 
difficulty = difficulty.upper() 

if difficulty == 'E': 
    words = ['teeth', 'heart', 'police', 'select', 'monkey'] 
    chosen = random.choice(words) 
    letters = list(chosen) 
    random.shuffle(letters) 
    scrambled = ''.join(letters) 
else: 
    print("no good") 
    sys.exit(0) 

# Now the scrambled word fixed until the end of the game 

# Game loop 
print("Try to guess the word: ", scrambled, " (", tries_left, " tries left)") 

while tries_left > 0: 
    print(scrambled) 
    guess = input("> ") 

    if guess == chosen: 
     print("Congratulations!") 
     break 
    else: 
     print("You suck, try again?") 
     tries_left -= 1 

何かを理解できない場合は、私はあなたを助けてくれるでしょう。

+0

ありがとう、ありがとう大規模な助け!うまくいけば、私は媒体と難しい言葉で裂くことができます、これは私が自分でやろうとしたことの中で最も難しいことです、私は完全なnoobだと思われないようにここに投稿したくありませんでした私はもう一度感謝してうれしいです! –