I have a text file which I want to use as an input in my Hangman
game. Hangman is a word game. So the file contains 4 words just for testing purpose.
Contain of the file ["aback", "charge","flight","fruit"]
Code:
from google.colab import files
uploaded = files.upload()
To upload a file: (Using Panda)
import io
import pandas as pd
io.StringIO(uploaded["words.txt"].decode("utf-8"))
pd.read_csv(io.StringIO(uploaded["words.txt"].decode("utf-8")))
Upload file using file open command
file_name = "words.txt"
uploaded[file_name].decode("utf-8")
Hangman code: My code
I have commented out line 6 of linked code to get user input from the text file. However, I got
Please guess the letter: a
You have 4 left
Please guess the letter: b
You have 3 left
Please guess the letter: a
You have 2 left
Please guess the letter: c
You have 1 left
Please guess the letter: k
Sorry! you don't have more lives to continue the game
Correct word is: 0 ["aback"
Name: 0, dtype: object
As you can see although the computer guessed the word aback
but when I gave the same input it could not find the word. Additionally, the computer always randomly choose the first word that was aback.
I am sure that there is no bug in my hungman code as it has been tested. Therefore, I believe it is the file upload code that creates problem.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…