0

After the code runs all the way for the first time, I want it to be able to go back to the first input and allow the user to input another word.

Do I need to create a for or while loop around the program?

import re    
import requests

search = input('What word are you looking for?: ')
first = re.compile(search)

source = input ('Enter a web page: ')
r = requests.get(source)
ar = r.text

mo = first.findall(ar)
print (mo)

print('Frequency:', len(mo))
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135

2 Answers2

0

Yes, a loop is needed:

import re    
import requests

while True:
    search = input('What word are you looking for?: ')
    # per comment, adding a conditional stop point.
    if search == "stop":
        break

    first = re.compile(search)

    source = input ('Enter a web page: ')
    r = requests.get(source)
    ar = r.text

    mo = first.findall(ar)
    print (mo)

    print('Frequency:', len(mo))
Bill Chen
  • 1,699
  • 14
  • 24
0

Yep a while look is the way to go.

Make sure you don't have an infinite loop though. (You want some way to break out...

I would recommend just adding this to the search, so if they type exit it breaks out of the look. I have also added .lower() so Exit, EXIT, etc also work.

import re    
import requests

while True:
    search = input('What word are you looking for?: ')

    if search.lower() == "exit": #break out of the loop if they type 'exit'
        break

    first = re.compile(search)

    source = input ('Enter a web page: ')
    r = requests.get(source)
    ar = r.text

    mo = first.findall(ar)
    print (mo)

    print('Frequency:', len(mo))
Ben Rauzi
  • 564
  • 4
  • 13
  • 1
    This is just a copy of the [other, earlier answer](https://stackoverflow.com/a/58704544/2745495) – Gino Mempin Nov 05 '19 at 09:09
  • Well there wasn't another answer when I submitted it... They weren't far apart so that's probably why. (When I clicked on the question it was only 20 minutes old) – Ben Rauzi Nov 05 '19 at 23:59