Python Forum

Full Version: Building first python program. Need a little guidance.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey. I'm Chris. I'm new to this forum and pretty new to python too. I've learned enough that I want to try building a small program that I can actually use.

I'm trying to make a simple little flashcard program that will run in the terminal. I have the questions and answers saved in .txt files. I've figured out how to get python to display the text in a file, but am having trouble displaying one thing in the file at a time.

The answers are on the same line as the questions, separated by a couple tabs.
How do I get python to display the first question then wait til I press enter to display the answer, then wait again for enter to display the next question, and so forth?
(Oct-24-2018, 09:58 PM)Chrislw324 Wrote: [ -> ]I've figured out how to get python to display the text in a file, but am having trouble displaying one thing in the file at a time.
If you're doing file_object.read(), then you're doing it the hard way. The easy way to read files that's used 99% of the time, would already handle what you're trying to accomplish:
with open("my_file.txt") as my_file:
    for line in my_file:
        print(line)
That prints the entire file.
Yes, but it does so one thing at a time.

Please, show the code you've got now, what output you're getting, and what output you want to get.
The code I currently have is your suggestion which displays the entire file.

Suppose I had a file with questions and answers separated by two tabs:

Question 1 <tab> <tab> Answer 1
Question 2 <tab> <tab> Answer 2
etc..

I'd like the output to just display Questions 1, then wait for me to press enter and then display Answer 1.
Then when I press enter again, Question 2 is displayed.
This could give you a step in the right direction.
The csv module can also help, with the delimiter argument set to \t.
parts = line.split("\t")
print(parts)
(Oct-24-2018, 09:58 PM)Chrislw324 Wrote: [ -> ]I've learned enough that I want to try building a small program that I can actually use.

(Oct-25-2018, 01:20 AM)Chrislw324 Wrote: [ -> ]The code I currently have is your suggestion which displays the entire file.

You claim you have learned enough, to try and build small program (with little guidance probably), yet everything you have is the code suggested by nilamo? If this is not a homework, you should try to work on what you already have and use what you have learned so far - e.g. working with files, string methods, console output/input, loop/iteration, etc..