Prześlij pliki do 'Projekt2'
This commit is contained in:
parent
b9c69cac7e
commit
0906b45818
5
Projekt2/.gitignore
vendored
Normal file
5
Projekt2/.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
__pycache__/*
|
||||
*.pyc
|
||||
.#*
|
||||
.coverage
|
||||
htmlcov/*
|
2
Projekt2/README.md
Normal file
2
Projekt2/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
# Hangman
|
||||
Hangman Game
|
76
Projekt2/hangman.py
Normal file
76
Projekt2/hangman.py
Normal file
@ -0,0 +1,76 @@
|
||||
# hangman.py
|
||||
|
||||
import random
|
||||
|
||||
def get_secret_word(word_file="/usr/share/dict/words"):
|
||||
with open(word_file) as f:
|
||||
good_words = []
|
||||
for i in f:
|
||||
i = i.strip()
|
||||
if len(i) <= 6: # No short words
|
||||
continue
|
||||
if not i.isalpha(): # No punctuation
|
||||
continue
|
||||
if i[0].isupper(): # No proper nouns
|
||||
continue
|
||||
good_words.append(i)
|
||||
return random.choice(good_words)
|
||||
|
||||
def get_masked_word(word_file):
|
||||
mask_word="*" * len(word_file)
|
||||
return mask_word
|
||||
|
||||
def type_guess_word(word_file, guess_word, guessed_line):
|
||||
if guess_word in word_file:
|
||||
x = 0
|
||||
for i in word_file:
|
||||
if i == guess_word:
|
||||
guessed_line = guessed_line [0:x] + guess_word + guessed_line[x+1:]
|
||||
x = x+1
|
||||
else:
|
||||
print("Wrong guess")
|
||||
print("-----------------------------")
|
||||
return guessed_line
|
||||
|
||||
def user_input(input=input):
|
||||
letter = input("enter the chacracter=")
|
||||
return letter
|
||||
|
||||
|
||||
def n_main():
|
||||
print ("Welcome.")
|
||||
s_word =(get_secret_word())
|
||||
#print(s_word)
|
||||
guessed_line=(get_masked_word(s_word))
|
||||
print(get_masked_word(s_word))
|
||||
a =True
|
||||
tries=10
|
||||
guess_word_list = []
|
||||
while a:
|
||||
if guessed_line == s_word:
|
||||
print("\n\U0001F44D\U0001F44D\U0001F44D CONGRATULATION ..!! YOU WON THE GAME")
|
||||
break
|
||||
print("----------------------------------------")
|
||||
print("\nTries left = {}".format(tries))
|
||||
guess_word = user_input()
|
||||
if guess_word in guess_word_list:
|
||||
print("already guess")
|
||||
continue
|
||||
if guess_word.isdigit():
|
||||
print("digits not allowed")
|
||||
continue
|
||||
if len(guess_word) != 1:
|
||||
print("\n single char only...!!!\n")
|
||||
continue
|
||||
guess_word_list.append(guess_word)
|
||||
guessed_line = type_guess_word(s_word, guess_word,guessed_line)
|
||||
print (guessed_line)
|
||||
print ("already guessed word ={}".format(guess_word_list))
|
||||
print("========================================")
|
||||
tries = tries-1
|
||||
if (tries < 1):
|
||||
a= False
|
||||
print ("\n So tries left sorry..the secret word is {} \U0001F622 \U0001F622\n\n".format(s_word))
|
||||
|
||||
if __name__ == "__main__":
|
||||
n_main()
|
11
Projekt2/requirements.txt
Normal file
11
Projekt2/requirements.txt
Normal file
@ -0,0 +1,11 @@
|
||||
atomicwrites==1.3.0
|
||||
attrs==18.2.0
|
||||
coverage==4.5.2
|
||||
more-itertools==5.0.0
|
||||
pathlib2==2.3.3
|
||||
pkg-resources==0.0.0
|
||||
pluggy==0.8.1
|
||||
py==1.7.0
|
||||
pytest==4.2.0
|
||||
pytest-cov==2.6.1
|
||||
six==1.12.0
|
9
Projekt2/spec.txt
Normal file
9
Projekt2/spec.txt
Normal file
@ -0,0 +1,9 @@
|
||||
1. Select a random word from a dictionary (/usr/share/dict/words) (sudo apt-get install dictionaries-common). This is the secret word.
|
||||
2. Present the user with the following information
|
||||
1. Masked secret word (* for unguessed letters, actual letter for correctly guessed letters)
|
||||
2. Number of tries left (start with 10)
|
||||
3. Wrong guesses so far
|
||||
3. Ask user to guess a letter.
|
||||
4. Update the information and goto 2.
|
||||
5. If the user gets the word in less than 10 tries, print "Congratulations!" and quit
|
||||
6. Otherwise, print "Too bad! The secret word was ..." and quit.
|
Loading…
Reference in New Issue
Block a user