473,395 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Function not behaving same when called second time

In my guessing game, when the correct (five-letter) word is entered on the first try, it works fine (which means, it returns the correctly guessed word).

But, when the correct word is entered on any other than the first try, it is returning None instead.

Expand|Select|Wrap|Line Numbers
  1. import random
  2.  
  3.  
  4. def word_list():
  5.     with open("MyFile.txt", "r") as file:
  6.         allText = file.read()
  7.         return list(map(str, allText.split()))
  8.  
  9. def random_word():
  10.     rnd_word = (random.choice(word_list()))
  11.     return rnd_word
  12.  
  13. def is_real_word(guess, valid_words_list):
  14.     if guess in valid_words_list:
  15.         #check
  16.         print("Passed word from next guess: ",guess)
  17.         return True        
  18.     else:
  19.         #check
  20.         print("Passed word from next guess: ",guess)
  21.         print("That's not a real word!")
  22.         next_guess(valid_words_list)
  23.  
  24. def next_guess(word_list):       
  25.     ges_word = input("Please enter a guess: ")
  26.  
  27.     if is_real_word(ges_word, word_list):
  28.         #check
  29.         print("next guess true ges word: ",ges_word)
  30.         return ges_word 
  31.  
  32. def check_guess(s1, s2):
  33.     s = ''
  34.     valid_list = list(s2)
  35.  
  36.     for i in range(len(s1)):
  37.         if s1[i] in valid_list:
  38.             if s1[i] in s2:
  39.                 if i == s2.index(s1[i]):
  40.                     s+="x"
  41.                     valid_list.remove(s1[i])                    
  42.                 else:
  43.                     s+="o"
  44.                     valid_list.remove(s1[i])                  
  45.         else:
  46.             s+="_"            
  47.     return s
  48.  
  49.  
  50.  
  51. def play(): 
  52.  
  53.     guessed_word = next_guess(word_list())
  54.     #check
  55.     print("Passed from next guess: ",guessed_word)
  56.     correct_word = random_word()
  57.     outcome = check_guess(guessed_word, correct_word)
  58.  
  59.     print("Random word: ", correct_word)
  60.     print(outcome)
  61.  
  62. play()
  63.  
Behavior when the correct letter is entered on the first try:
Please enter a guess: kumar
Passed word from next guess: kumar
next guess true ges word: kumar
Passed from next guess: kumar
Random word: taunt
_o_o_

Behavior when the correct word is entered on the second try:
Please enter a guess: aaaaa
Passed word from next guess: aaaaa
That's not a real word!
Please enter a guess: kumar
Passed word from next guess: kumar
next guess true ges word: kumar
Passed from next guess: None
May 29 '22 #1
0 9392

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: thakadu | last post by:
The following code works as expected when run in the main body of a python script (ver 2.3.5) on OpenBSD v3.8. but when it is in the body of a function definition it does not work. It does not...
3
by: Patrice | last post by:
Hi, I would to call fopen function several time in my application. This application permits to read files which path is registered in a configuration file. For exemple: File 1 = toto.txt File 2...
2
by: FrzzMan | last post by:
The first time I called this function, everything went well, but the second time I called it. An Exception thrown, do you know why? An unhandled exception of type 'System.IO.IOException' occurred...
8
by: bidllc | last post by:
I have a funtion that works fine and dandy when called from anywhere in my app. It will NOT work when called from inside the class in which it resides. This is the function I'm calling:...
3
by: Amar | last post by:
I have a abc.PRG file in visual foxpro 8.0. I can run this file using visual foxpro environment and it creates a table X.dbf in the same folder where this program file is and populates some data...
1
by: bhavin30 | last post by:
Hi Guys, I have a quick question. I think code will explain it the best. This is what I am doing for one of the columns during databinding on my grid: <asp:TemplateColumn> <HeaderStyle...
1
by: thulaseeram | last post by:
hi, i am using the following code for uploading <div id="iframe" name="iframe" style="width:850px; height:120px;"> <iframe name="attach_frame" id="attach_frame"...
7
by: Peter Laan | last post by:
Is there a simple way to encapsulate the functionality to redo a method call a second time in case a specific exception is thrown? We are sending commands to an external system and if the sessionId...
2
by: david l smith | last post by:
Hi, The following code snippet works on page load (it displays an anchor link using document.write), works when the anchor is clicked on the first time, but does not work when the anchor link is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.