guys I'm new to python and I decided to learn it via doing some project myself so I started working on Hangman game is there somebody willing to chceck my code for some common mistakes and what could be better done than it is, btw the works fine.
import random
def rand_word(version):
if version.lower() == "slovak":
words = open("Slova", "r")
all_lines = words.readlines()
line_num = random.randint(0, len(all_lines))
words.close()
return all_lines[line_num]
elif version.lower() == "english":
words = open("Words", "r")
all_lines = words.readlines()
line_num = random.randint(0, len(all_lines))
words.close()
return all_lines[line_num]
else:
print("Wrong input! [FATAL ERROR]")
input("Press enter to EXIT!")
exit()
def draw(what_to_do):
HANGMANPICS = ['''
+---+
| |
|
|
|
|
=========''', '''
+---+
| |
O |
|
|
|
=========''', '''
+---+
| |
O |
| |
|
|
=========''', '''
+---+
| |
O |
/| |
|
|
=========''', '''
+---+
| |
O |
/|\ |
|
|
=========''', '''
+---+
| |
O |
/|\ |
/ |
|
=========''', '''
+---+
| |
O |
/|\ |
/ \ |
|
=========''']
if what_to_do == 1:
print(HANGMANPICS[what_to_do])
elif what_to_do == 2:
print(HANGMANPICS[what_to_do])
elif what_to_do == 3:
print(HANGMANPICS[what_to_do])
elif what_to_do == 4:
print(HANGMANPICS[what_to_do])
elif what_to_do == 5:
print(HANGMANPICS[what_to_do])
elif what_to_do == 6:
print(HANGMANPICS[what_to_do])
elif what_to_do == 7:
print(HANGMANPICS[what_to_do])
else:
print(HANGMANPICS[0])
def list_fill(size):
i = 0
size -= 1
while i < size:
positions.append("_")
i += 1
print("HANGMAN")
print("You have 6 tries to guess the correct word!")
dictionary = input("Chose dictionary Slovak or English: ")
positions = []
tries = 0
win_con = 1
addition = False
temp_word = rand_word(dictionary)
#print(temp_word)
list_fill(len(temp_word))
while tries < 6:
counter = 0
draw(tries)
print(*positions)
user_letter = input("Enter a letter: ")
for letter in temp_word:
if letter == user_letter:
positions[counter] = letter
win_con += 1
tries -= 1
else:
addition = True
counter += 1
if addition:
tries += 1
if win_con == len(temp_word):
print("You have won!")
print(*positions)
input("Press enter to EXIT!")
exit()
print("You have run out of tries! ")
print(f"The word was {temp_word.upper()}")
input("Press enter to EXIT!")
version = version.lower()
and you don't have to repeatelower()
in allif/else
\$\endgroup\$HANGMANPICS
has elements with indexes from 0 to 6 but you use index 7 \$\endgroup\$