嗨,我完全是新的编程,并一直试图教自己的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)
正如你看到的,我只得到了如很简单,我试图一件一件做,并设法克服其他问题,但我似乎无法修复我所拥有的。任何帮助将不胜感激与我遇到的问题或任何其他问题,你可能会在我的代码中发现。
然后选择你的话/争夺_before_重试循环... –
你可能想缩进if区块,以便它被while循环拾取 – Ohjeah
@Ohjeah我认为,但错误描述清楚地表明它是一个缩进发布时出错。 –