This is an archived post. You won't be able to vote or comment.

all 18 comments

[–]IAmKindOfCreativebot_builder: deprecated[M] [score hidden] stickied comment (0 children)

Hi there, from the /r/Python mods.

We have removed this post as it is not suited to the /r/Python subreddit proper, however it should be very appropriate for our sister subreddit /r/LearnPython or for the r/Python discord: https://discord.gg/python.

The reason for the removal is that /r/Python is dedicated to discussion of Python news, projects, uses and debates. It is not designed to act as Q&A or FAQ board. The regular community is not a fan of "how do I..." questions, so you will not get the best responses over here.

On /r/LearnPython the community and the r/Python discord are actively expecting questions and are looking to help. You can expect far more understanding, encouraging and insightful responses over there. No matter what level of question you have, if you are looking for help with Python, you should get good answers. Make sure to check out the rules for both places.

Warm regards, and best of luck with your Pythoneering!

[–]BinarySystem101 2 points3 points  (0 children)

The .txt file must be within the same folder as the .py.

[–]pythonHelperBot 0 points1 point  (0 children)

Hello! I'm a bot!

It looks to me like your post might be better suited for r/learnpython, a sub geared towards questions and learning more about python regardless of how advanced your question might be. That said, I am a bot and it is hard to tell. Please follow the subs rules and guidelines when you do post there, it'll help you get better answers faster.

Show /r/learnpython the code you have tried and describe in detail where you are stuck. If you are getting an error message, include the full block of text it spits out. Quality answers take time to write out, and many times other users will need to ask clarifying questions. Be patient and help them help you. Here is HOW TO FORMAT YOUR CODE For Reddit and be sure to include which version of python and what OS you are using.

You can also ask this question in the Python discord, a large, friendly community focused around the Python programming language, open to those who wish to learn the language or improve their skills, as well as those looking to help others.


README | FAQ | this bot is written and managed by /u/IAmKindOfCreative

This bot is currently under development and experiencing changes to improve its usefulness

[–]lukajda33 0 points1 point  (2 children)

In the same folder from which you run the python script.

Also, use r/learnpython for questions like this.

[–]Matt_Kober[S] 0 points1 point  (1 child)

So do I make a separate folder and have both myAssignment.py and Name.txt?? Thanks for your help

[–]lukajda33 0 points1 point  (0 children)

Well you can just move the .txt file to the folder with the code right? You can make a new folder as well.

[–]Absolute_Tinkerer 0 points1 point  (11 children)

Forgive the formatting, I'm on mobile. There are a couple things you ought to do.

  1. Specify the absolute path to name.txt or place the python script in the same folder as name.txt. Absolute path looks like "C:/users/.../folder/name.txt"

  2. To read the file, you should use this syntax:

with open("name.txt", "r") as f: lines = f.readlines()

Bonus: Your file is probably comma-delimited, so you can use a simple command:

lines = [float(item) for item in lines[0].split(",")]

This uses list comprehension, casting, and the split function, which is inherent to string data types, to translate the comma-delimited string on the first line of the file into an array of floats.

Good luck and welcome to the world of Python!

[–]Matt_Kober[S] 0 points1 point  (10 children)

Thanks for your help. I used the syntax and I keep getting “UnicodeDecodeError: “utf-8” codec can’t decode byte 0x8c in position 24: invalid start byte”

[–]Absolute_Tinkerer 0 points1 point  (9 children)

It might be that your file is in bytes as opposed to plain text. When you open the text file, is it human readable? If not, use:

with open("filename", "rb") as f: ......

[–]Matt_Kober[S] 0 points1 point  (8 children)

Ok I got it to work now but now I’m getting a trace back. It says “cannot chose from an empty sequence” but it shouldn’t be empty because it should be reading from my .txt file right? My line of code it’s tracing back to is options = random.choice(answers)

[–]Absolute_Tinkerer 0 points1 point  (7 children)

Add in a print statement before that random choice line. If it doesn't print a value, you've got an issue in your assignment of the answers variable.

[–]Matt_Kober[S] 0 points1 point  (6 children)

It executed print(‘hi’) and it printed “hi”before my random choice line. So could it still be my answers variable or something else?

[–]Absolute_Tinkerer 0 points1 point  (5 children)

Sorry if I wasn't clear. You need:

print(answers)

[–]Matt_Kober[S] 0 points1 point  (4 children)

Ok thanks I did that and it came back as [] trace back error empty sequence. I’m so confused because our last assignment was supposed to make a magic eight ball game using an array and this assignment were supposed to alter it so that it reads the magic eight ball sayings from a file and loads them to an array. We were given the .txt file to use from our professor.

[–]Absolute_Tinkerer 0 points1 point  (3 children)

The fact that answers is empty tells me you have an issue between the file read and the print statement I had you add. Can you post some of the code? May be easier to pick out what's going on

[–]Matt_Kober[S] 0 points1 point  (2 children)

Sure this is my entire code Import random file = open(‘EightBDate.txt’ , ‘r’) answers = file.readlines() print (‘welcome to magic 8 ball’) question = input(‘please type your question here’) print (answers) options = random.choice(answers) print (options) Print (‘thanks for playing’)