0

i want to program that take user input line:, and check that each line is correct or not from a speech.txt. if the line is correct with line in file it should proceed and ask input again line: check the line again, if the line is wrong print the correct line, and if the user types LINE! then print the correct line from file, and print GOOD when lines finished. so FAR i have made this program but the last some loops are useless even if the lines in file are finished

f=open('speech.txt')
while True:
    userline=input("line: ")
    for line in f:
        line=line.strip()
        if line.lower() == userline.lower():
            userline=input("line: ")
        elif userline=="LINE!":
            print(line)
    print("Good!")
    break
1
  • I'm not clear on your question. What isn't working? The code you have here seems like it should do what you want (except for printing correct lines when the user is wrong), although it's a bit awkward to look at... Commented Aug 29, 2013 at 18:01

4 Answers 4

2

If I understood your question right, this would be what you are looking for:

try:
    _input = raw_input
except:
    _input = input

with open('a') as a:
    for i in a:
        line = i.rstrip('\n')
        while True:
            user = _input("enter line: ")
            if user == "LINE!":
                print('%s\n' % line)
                break
            if line == user:
                break
            print("No! Try again...")
    print("Good!")
Sign up to request clarification or add additional context in comments.

1 Comment

My bad. Did not notice the python-3.x tag. Changed my answer to work on both python 2.x and 3.x
0

The short answer is: to stop a loop, use the break statement eg

while True:
   q = input("What is your quest? ")
   if q == "To seek the holy grail":
       break
   print("That's not right, try again")
#Program continues.

In your case I would consider thinking about the logic of the loops. You seem to have nested loops unnecessarily. A single for loop through the file should be enough.

Comments

0

It seems to me like you're using more loops than you need. This is how I'd write it, if I understand your constraints correctly.

with open('speech.txt') as f:
    for line in f:
        line = line
        userline = input("line: ")
        if userline == 'LINE!':
            print(line)
        elif userline.strip().lower() == line.strip().lower():
            continue # Correct, so go on to the next line of the file
        else:
            print("Nope! Correct was:")
            print(line)
    print('FINISHED') # I wouldn't use "GOOD" unless the user gets everything right!

The trick here is the continue statement. This skips ahead to the next iteration of the loop if the user was correct. To break out of a loop entirely (say, if you wanted to simply stop when the user gets a line wrong), you would use the break statement.

I don't know what your original while loop was intended to do, but it wasn't doing anything at all in your code, and you don't need two loops just to go through a file once.

Comments

0

To stop a loop you should use: Break.

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.