FOLLOW ME ON :

Go to My Social Media Pages

Select an option:

python QUEUE AND STACK

Python Code

Python Code

from queue import Queue from collections import deque longest = 0 lword = '' w = '' q = Queue() s = deque() print('Enter 10 words:') for _ in range(10): s.append(input('')) for _ in range(10): w = s.pop() if len(w) > longest: lword = w longest = len(w) print(f"The longest word is {lword}") input('Enter to close')

The code snippet prompts the user to enter 10 words and stores them in a deque object called s. It then iterates over the words, comparing their lengths to find the longest word. The longest word and its length are stored in variables lword and longest, respectively. Finally, the code displays the longest word using an f-string and waits for user input before closing the program.

Comments

game