import random
def random_Words():
print("Lets play HANGMAN")
option = input("Please Chose option \n [1] Animals.\n [2] Countries \n [3] Flowers \n [4] Pakistan's Cities \n");
i=0;
x= "";
if(option=='1'):
path= "code/hangman/Words.Animals"
if(option=='2'):
path="code/hangman/Words.Countries"
if(option=='3'):
path="code/hangman/Words.Flowers"
if(option=='4'):
path="code/hangman/Words.PakCities"
return random.choice(open(path).readlines());
def HangmanGuess(RandomWord, GuessedWord, Chances):
Letters_Guessed = []
if(Chances > 0):
i=0;
GuessOption = input("Guess a Letter :");
if(GuessOption == Letters_Guessed):
print("Already Guessed")
GuessOptionValid= False;
for i in range(len(RandomWord)-1):
if(RandomWord[i] == GuessOption):
GuessedWord= GuessedWord[:i] + GuessOption + GuessedWord[i+1:];
GuessOptionValid =True;
print(RandomWord);
print(GuessedWord);
if(GuessOptionValid):
HangmanGuess(RandomWord,GuessedWord, Chances);
else:
HangmanGuess(RandomWord,GuessedWord, Chances-1);
else:
print('You have lost!!')
Chances = 5
RandomWord = random_Words()
GuessedWord = ''
i=0
for i in range(len(RandomWord)-1):
GuessedWord+= '_'
def Hangman_Drawing(RandomWord,GuessedWord, Chances):
x= HangmanGuess(RandomWord,GuessedWord, Chances);
x = ''
if(GuessedWord!= RandomWord[i]):
if(Chances==4):
print("----------");
print("| | ");
print("| ");
print("| ");
print("| ");
print("| ");
print("| ");
print("-----------");
if(Chances==3):
print("----------");
print("| | ");
print("| O ");
print("| ");
print("| ");
print("| ");
print("| ");
print("-----------");
if(Chances==2):
print("----------");
print("| | ");
print("| O ");
print("| | ");
print("| ");
print("| ");
print("| ");
print("-----------");
if(Chances==1):
print("----------");
print("| | ");
print("| O ");
print("| | ");
print("| /\ ");
print("| ");
print("| ");
print("-----------");
if(Chances==0):
print("----------");
print("| | ");
print("| O ");
print("| --|-- ");
print("| /\ ");
print("| ");
print("| ");
print("-----------");
print(Hangman_Drawing(RandomWord,GuessedWord, Chances));
好傢伙, 蔭停留在一個問題,我想提出一個支票驗證,如果當字母「猜」是被輸入任何字母它檢查是否該信件之前已被輸入,因此提供打印的信息,以提醒用戶,如果發現 我的代碼有'檢查'代碼,但我不知道什麼是缺少或爲什麼它不工作,而其餘的代碼是非常好。 對不起iam非工程師,所以如果有任何信息丟失,請原諒 任何幫助,將不勝感激。 謝謝。劊子手:不重複孰料信再次
與您的實際問題無關,但在Python中行尾不需要分號。他們*被允許*,並且讓你在單行上結合多個語句(儘管這通常是不好的風格),但他們從來沒有必要。 – Blckknght