30.2k views
0 votes
4.5 Code Practice

Write a loop that inputs words until the user enters STOP. After each input, the program should number each entry and print in this format:
(This code must be in python, also if a minor explanation can be added to the bottom that would be nice. But also long as u can help I will be fine)

User Rhunwicks
by
4.7k points

2 Answers

7 votes

Answer:

c = 0

word = input("Enter a word: ")

while word!="STOP":

c = c+1

print("#" + str(c) + ": You entered "+str(word))

word = input("Please enter the next word: ")

print("All done. "+str(c)+" words entered.")

User Dave DeCaprio
by
5.1k points
2 votes

i = 0

while True:

user_input = input("Please enter the next word: ")

if user_input == "STOP":

break

i += 1

print("#{}: You entered {}".format(i,user_input))

print("All done. {} words entered.".format(i))

First we set i equal to zero so that we can keep track of how many words we input.

We set while True so that its a continuous loop until a certain condition is met to break out of the loop.

user_input is set equal to whatever word the user enters.

our if statement tells us to break out of the while loop if the user inputs "STOP"

If the user does not enter STOP i is set equal to itself plus 1. This just means we add one to i for every new word entered.

Then we print whichever word is entered.

After the while loop, we print All done and the quantity of words entered.

User Kevin Dostalek
by
4.1k points