Trying to create a FIFO function that iterates on a list of inputted integers, but every time I call the function I get an infinite loop.
I start with two empty global scope lists named cache and requests, with a while loop that gets the user to input integers that are entered into requests.
cache = []
requests = []
while len(requests) < 9999999999:
prompt = int(input("Please input your page requests: "))
requests.append(prompt)
print(requests)
if prompt == 0:
break
Once a 0 is entered the user is then taken to a menu asking them which function they would like to call
menu = input("Choose 1 for FIFO, 2 for LFU, or Q to quit:")
while (menu != "Q"):
if (menu == "1"):
FIFO(cache, requests)
elif (menu == "2"):
LFU(cache, requests)
else:
menu = input("Please choose a valid option:")
If the user chooses "1", the FIFO() function is called, and here is where I get the infinite loop
def FIFO(cache, requests):
for request in requests:
if request not in cache:
print("Miss!")
cache.append(request)
elif request in cache:
print("Hit!")
elif len(cache) > 8:
del cache[0]
elif requests == 0:
print(cache)
return
For a bit more context, I want elements of the list requests to be appended to the list cache one at a time. If an element is added to cache and its different from all the elements currently in cache I want the function to print "Miss!".
Alternatively, if the appended element of the list requests is already in cache, "Hit!" should be printed.
Lastly if the length of the cache is greater than 8 (len(cache) > 8), I want it to remove the first element in the list cache (cache[0]).
Does anyone know why I keep getting an infinite loop? And please feel free to say what else is wrong with my code or what could be better. Really keen to learn from you all!