Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

python - File upload using google CoLab throws error

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...