2

So I'm creating an 8 ball program that prompts the user for a question will then spit out a response. The project I have states that the answers must be in txt file, so I created this

file = open("Ball_response.txt","w")

file.write("YES, OF COURSE!")
file.write("WITHOUT A DOUBT, YES")
file.write("YOU CAN COUNT ON IT.")
file.write("FOR SURE!")
file.write("ASK ME LATER")
file.write("I AM NOT SURE")
file.write("I CAN'T TELL YOU RIGHT NOW?")
file.write("I WILL TELL YOU AFTER MY NAP")
file.write("NO WAY!")
file.write("I DON'T THINK SO")
file.write("WITHOUT A DOUBT, NO.")
file.write("THE ANSWER IS CLEARLY NO")

file.close()

The I want to call the list here

import random 

# Reading the Ball_response file
def main():
    input_file = open('Ball_response.txt', 'r')
    line = input_file.readline()
    print(line[0])

main()

But when I run the program, it only prints out "Y". I want

   0 -  Yes, Of Course
   1 -  Without a Doubt, yes
   2 -  You can count on it

etc..... How can I accomplish this? I feel like there is something I'm not understanding

7
  • what you mean by "when I click run the 0 only " Commented Mar 21, 2018 at 17:04
  • 2
    Based on how you write the ball_response.txt the line n = int(line) will always error since non of the lines are int. Also you are reading only one line then it looks like you are trying to loop through the lines? Commented Mar 21, 2018 at 17:05
  • 1
    line = "YES, OF COURSE!WITHOUT A DOUBT, YESYOU CAN COUNT ON IT.FOR SURE!ASK ME LATERI AM NOT SUREI CAN'T TELL YOU RIGHT NOW?I WILL TELL YOU AFTER MY NAPNO WAY!I DON'T THINK SOWITHOUT A DOUBT, NO.THE ANSWER IS CLEARLY NO" So of course, when you print line[0] you get "Y" Commented Mar 21, 2018 at 17:05
  • @ZackTarr My apologies, the n-int(line) should not have been in there Commented Mar 21, 2018 at 17:08
  • 1
    Don't forget to upvote answers that helped you and tick the one you think helped you solve your problem the best. You may want to read stackoverflow.com/tour Commented Mar 21, 2018 at 17:45

3 Answers 3

2

In order to get each response on a different line you need to change the way you are writing your file.

file = open("Ball_response.txt","w")

file.write("YES, OF COURSE!\n")
file.write("WITHOUT A DOUBT, YES\n")
// etc

file.close()

Then in your function main since you want to print the whole line you need to do print(line).

import random

def main():
    input_file = open('Ball_response.txt', 'r')
    rand_idx = random.randrange(12)

    for i,line in enumerate(input_file):
      if i == rand_idx:
        print(str(i) + " - " + line.strip('\n'))

This will print (for example):

7 - I WILL TELL YOU AFTER MY NAP

Sign up to request clarification or add additional context in comments.

4 Comments

Okay! So if I wanted to make it so it only prints one of these answers randomly and not all 11 can I just add a random function?
Thank you for the help, I'm still getting a input_file not defined error but maybe I should problem solve from there,
@AySeven That's very strange. Have you executed the first part with file = open(...) .... file.close(). Is the file 'Ball_response.txt' in your working directory?
I apologize. When I took out the debugging and fixed the formatting under my main function it was working. Thank you very much for the help
1

You might want to fix your code first.

While writing into file add \n in each file.write so that all lines go to new lines like this

file.write("YES, OF COURSE!\n")

For more efficiency store all these strings in a list and use file.writelines(list)

lines = ["YES, OF COURSE!\n","WITHOUT A DOUBT, YES\n","YOU CAN COUNT ON IT.\n"]

file.writelines(lines)

For reading a file line by line do this.

def main():
    input_file = open('Ball_response.txt', 'r')
    i = 0
    for line in input_file:
        print(str(i) + ' ' + line)
        i = i+1

One can also do this to automatically enumerate

for i,line in enumerate(input_file):
    print(str(i) + ' ' + line)

Output will be like

0 YES, OF COURSE!

1 WITHOUT A DOUBT, YES

2 YOU CAN COUNT ON IT.

2 Comments

Okay, this makes more sense, but I still get a input_file is not defined error
Edited with more details
0

You should use print(line) instead of print(line[0]). You are only getting Y because it is the [0]'th element in the string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.