Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...