2017-03-01 96 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

谢谢,这就是一个巨大的帮助!希望现在我可以用中度和硬性的话来说明,这是我尝试独自做的最难的事情,我不想在这里发布它,因为看起来我不是一个完整的小白,但现在我很高兴我再次感谢! –