2017-03-01 151 views
0

嗨,我完全是新的編程,並一直試圖教自己的Python,我一直在嘗試創建一個程序,選擇一個字,然後洗牌的字母,並提示用戶輸入他們的猜測3次嘗試。我遇到的問題是,當一個錯誤的答案是改組所選單詞中的字母輸入或返回一個完全不同的字,這裏是我的代碼:隨機文字遊戲蟒蛇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

你可能想縮進if區塊,以便它被while循環拾取 – 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

謝謝,這就是一個巨大的幫助!希望現在我可以用中度和硬性的話來說明,這是我嘗試獨自做的最難的事情,我不想在這裏發佈它,因爲看起來我不是一個完整的小白,但現在我很高興我再次感謝! –