2014 dxdy logo

Научный форум dxdy

Математика, Физика, Computer Science, Machine Learning, LaTeX, Механика и Техника, Химия,
Биология и Медицина, Экономика и Финансовая Математика, Гуманитарные науки




Начать новую тему Ответить на тему
 
 Python Hangman Game Program Error
Сообщение17.05.2023, 15:18 
Аватара пользователя


20/12/22
14
Haldwani
Hello this is Gulshan Negi
Well, I am wiriting a program for making hangman game in Python , but it shows some error at the time of its execution.
Here is my source code:

import random
def select_word():
words_in_computer_memory = ['magazine','stars','computer','python','organisation']
word = random.choice(words_in_computer_memory)
return word
def is_gussed(word, guessed_letter_list):
count=0
for letters in word:
if letters in guessed_letter_list:
count+=1
if count==len(word):
return True
else:
return False
def guessed_word(word, guessed_letter_list):
string=""
for key in word:
if key in guessed_letter_list:
string+=key
else:
string+="_ "
return string
def available_letters(guessed_letter_list):

string=""
count=0:
s='abcdefghijklmnopqrstuvwxyz'
for letter in s:
if letter in guessed_letter_list:
count+=1
else:
string+=letter
return string
def hangman_game(word):
length=len(word)
print('''------------------WELCOME TO HANGMAN GAME---------------------------
O
/|\
/ \
''')
print("The word you have to guess is of ",length, "letters long.")
chances=2*len(word)
i=0
guessed_letter_list=[]
while (chances!=0):

if word!=guessed_word(word, guessed_letter_list):
print("You Got", chances, "Chances.")
print("Letters you can enter should be from these ",available_letters(guessed_letter_list))
guess=input("ENTER A LETTER ")
print('\n'*50)

guessInLowerCase = guess[0].lower()
if guessInLowerCase in guessed_letter_list:
print("SORRY! YOU HAVE GUSSED THIS LETTER ALREADY! ",guessed_word(word, guessed_letter_list))
elif guessInLowerCase not in word:
print(" SORRY! THE LETTER IS NOT IN WORD",guessed_word(word, guessed_letter_list))
chances-=1
else:
guessed_letter_list.append(guessInLowerCase)
print("NICE YOU GUSESSED THE RIGHT LETTER! ",guessed_word(word, guessed_letter_list))

elif word==guessed_word(word, guessed_letter_list):
print("YOU WON!")
break

else:
print('''
********************************************
YOU LOSS!!
O
/|\
/ \
******************************************''')
print('The word was',word,)

word = select_word()
hangman_game(word)

Well, I also checked and took a reference from https://www.techgeekbuzz.com/blog/hangman-game-in-python/, but ther is some syntax error while execution. Can anyone give their suggestions on this?
Thanks

 Профиль  
                  
 
 Re: Python Hangman Game Program Error
Сообщение17.05.2023, 15:29 
Заслуженный участник
Аватара пользователя


16/07/14
9143
Цюрих
Please post your code using [syntax] tag, cite errors you get, and include your attempts to understand their cause.

 Профиль  
                  
 
 Re: Python Hangman Game Program Error
Сообщение19.05.2023, 10:07 
Аватара пользователя


20/12/22
14
Haldwani
Thanks a lot for your time and kind response. Well, I will keet it in mind in future. Secondly I am getting syntax error in count function.

 Профиль  
                  
 
 Re: Python Hangman Game Program Error
Сообщение19.05.2023, 10:35 


05/09/16
12056
gulshan212 в сообщении #1594419 писал(а):
Well, I will keet it in mind in future.

Okay, so this topic goes nowhere, then?
Python requires indentation which isn't present in code you posted, so it's not really possible to say where the error is.
So if you still do need any help, please do what mihaild did suggest.

 Профиль  
                  
 
 Re: Python Hangman Game Program Error
Сообщение19.05.2023, 11:45 
Аватара пользователя


20/12/22
14
Haldwani
Thanks for your response but I am not able to see edit button.

 Профиль  
                  
 
 Re: Python Hangman Game Program Error
Сообщение19.05.2023, 11:56 


05/09/16
12056
gulshan212 в сообщении #1594423 писал(а):
Thanks for your response but I am not able to see edit button.

It's gone because of timeout, you can't edit your message after about an hour. Just post new message here in this topic.

 Профиль  
                  
 
 Re: Python Hangman Game Program Error
Сообщение19.05.2023, 11:56 
Заслуженный участник
Аватара пользователя


16/07/14
9143
Цюрих
You can post code with tags in a new message, or ask moderator (Ende) to move your topic temporarily to "quarantine", where you will be able to edit it (in other places you can't edit posts older than I think 1 hour).

 Профиль  
                  
 
 Re: Python Hangman Game Program Error
Сообщение19.05.2023, 11:59 
Аватара пользователя


20/12/22
14
Haldwani
Please consider this one.

код: [ скачать ] [ спрятать ]
Используется синтаксис Python
import random
def select_word():
words_in_computer_memory = ['magazine','stars','computer','python','organisation']
word = random.choice(words_in_computer_memory)
return word
def is_gussed(word, guessed_letter_list):
count=0
for letters in word:
if letters in guessed_letter_list:
count+=1
if count==len(word):
return True
else:
return False
def guessed_word(word, guessed_letter_list):
string=""
for key in word:
if key in guessed_letter_list:
string+=key
else:
string+="_ "
return string
def available_letters(guessed_letter_list):

string=""
count=0:
s='abcdefghijklmnopqrstuvwxyz'
for letter in s:
if letter in guessed_letter_list:
count+=1
else:
string+=letter
return string
def hangman_game(word):
length=len(word)
print('''------------------WELCOME TO HANGMAN GAME---------------------------
O
/|\
/ \
'''
)
print("The word you have to guess is of ",length, "letters long.")
chances=2*len(word)
i=0
guessed_letter_list=[]
while (chances!=0):

if word!=guessed_word(word, guessed_letter_list):
print("You Got", chances, "Chances.")
print("Letters you can enter should be from these ",available_letters(guessed_letter_list))
guess=input("ENTER A LETTER ")
print('\n'*50)

guessInLowerCase = guess[0].lower()
if guessInLowerCase in guessed_letter_list:
print("SORRY! YOU HAVE GUSSED THIS LETTER ALREADY! ",guessed_word(word, guessed_letter_list))
elif guessInLowerCase not in word:
print(" SORRY! THE LETTER IS NOT IN WORD",guessed_word(word, guessed_letter_list))
chances-=1
else:
guessed_letter_list.append(guessInLowerCase)
print("NICE YOU GUSESSED THE RIGHT LETTER! ",guessed_word(word, guessed_letter_list))

elif word==guessed_word(word, guessed_letter_list):
print("YOU WON!")
break

else:
print('''
********************************************
YOU LOSS!!
O
/|\
/ \
******************************************'''
)
print('The word was',word,)

word = select_word()
hangman_game(word)


Well, I also checked and took a reference from https://www.techgeekbuzz.com/blog/hangm ... in-python/, but ther is some syntax error while execution. Can anyone give their suggestions on this?
Thanks

 Профиль  
                  
 
 Re: Python Hangman Game Program Error
Сообщение19.05.2023, 12:08 
Заслуженный участник
Аватара пользователя


16/07/14
9143
Цюрих
Seems that you copied your code from your first message, where indentation was already lost. Please copy from your local data, with indentation.
You can use "Preview" button to check how will your message look before sending it.

 Профиль  
                  
 
 Re: Python Hangman Game Program Error
Сообщение19.05.2023, 12:21 


05/09/16
12056
gulshan212 в сообщении #1594427 писал(а):
I also checked and took a reference from https://www.techgeekbuzz.com/blog/hangm ... in-python/ , but ther is some syntax error while execution.

Okay. I took the source code from the link you provided and posted it below.

код: [ скачать ] [ спрятать ]
Используется синтаксис Python
  1. import random
  2. def select_word():
  3.     words_in_computer_memory = ['magazine','stars','computer','python','organisation']
  4.     word = random.choice(words_in_computer_memory)
  5.     return word
  6. def is_gussed(word, guessed_letter_list):
  7.     count=0
  8.     for letters in word:
  9.         if letters in guessed_letter_list:
  10.             count+=1
  11.     if count==len(word):
  12.         return True
  13.     else:
  14.         return False
  15. def guessed_word(word, guessed_letter_list):
  16.     string=""
  17.     for key in word:
  18.         if key in guessed_letter_list:
  19.             string+=key
  20.         else:
  21.             string+="_ "
  22.     return string
  23. def available_letters(guessed_letter_list):
  24.  
  25.     string=""
  26.     count=0
  27.     s='abcdefghijklmnopqrstuvwxyz'
  28.     for letter in s:
  29.         if letter in guessed_letter_list:
  30.             count+=1
  31.         else:
  32.             string+=letter
  33.     return string
  34. def hangman_game(word):  
  35.     length=len(word)
  36.     print('''------------------WELCOME TO HANGMAN GAME---------------------------
  37.                                    O  
  38.                                   /|\
  39.                                   / \
  40.        ''')
  41.     print("The word you have to guess is of ",length, "letters long.")
  42.     chances=2*len(word)
  43.     i=0
  44.     guessed_letter_list=[]
  45.     while (chances!=0):    
  46.  
  47.         if word!=guessed_word(word, guessed_letter_list):
  48.             print("You Got", chances, "Chances.")
  49.             print("Letters you can enter should be from these ",available_letters(guessed_letter_list))
  50.             guess=input("ENTER A LETTER ")
  51.             print('\n'*50)
  52.  
  53.             guessInLowerCase = guess[0].lower()      
  54.             if guessInLowerCase  in guessed_letter_list:
  55.                 print("SORRY! YOU HAVE GUSSED THIS LETTER ALREADY! ",guessed_word(word, guessed_letter_list))
  56.             elif guessInLowerCase not in word:
  57.                 print(" SORRY! THE LETTER IS NOT IN WORD",guessed_word(word, guessed_letter_list))
  58.                 chances-=1
  59.             else:
  60.                 guessed_letter_list.append(guessInLowerCase)
  61.                 print("NICE YOU GUSESSED THE RIGHT LETTER! ",guessed_word(word, guessed_letter_list))
  62.            
  63.         elif word==guessed_word(word, guessed_letter_list):
  64.             print("YOU WON!")
  65.             break
  66.  
  67.     else:
  68.         print('''
  69.        ********************************************
  70.   YOU LOSS!!
  71.                                 O
  72.                                /|\
  73.                                / \
  74.     ******************************************''')
  75.         print('The word was',word,)
  76.  
  77. word = select_word()
  78. hangman_game(word)

That code compiles and runs okay for example here https://www.programiz.com/python-progra ... -compiler/ if you just copy-paste it.

 Профиль  
                  
 
 Re: Python Hangman Game Program Error
Сообщение19.05.2023, 13:13 
Экс-модератор
Аватара пользователя


23/12/05
12063
gulshan212 в сообщении #1594427 писал(а):
but ther is some syntax error while execution

Could you copy this error message ?
I tried the code from the previous post and it works w/o syntax errors.

 Профиль  
                  
Показать сообщения за:  Поле сортировки  
Начать новую тему Ответить на тему  [ Сообщений: 11 ] 

Модераторы: Karan, Toucan, PAV, maxal, Супермодераторы



Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей


Вы не можете начинать темы
Вы не можете отвечать на сообщения
Вы не можете редактировать свои сообщения
Вы не можете удалять свои сообщения
Вы не можете добавлять вложения

Найти:
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group