Introduction
This tutorial will provide a step-by-step guide on how to create a loop in a program that will continue until the user types in the word “Stop”. This is a useful technique for creating interactive programs that allow the user to control the flow of the program. We will be using a while loop to achieve this, and will also discuss how to handle any errors that may occur.
Solution
while True:
user_input = input(“Type something: “)
if user_input == “Stop”:
break
else:
print(“You typed:”, user_input)
Your scope is screwed up all over the place, need to make sure the complete if/else is enclosed with {}, as it is most of your else’s don’t have anything to connect to.
while (!sentenceInput.equals ("STOP")) {
if (sentencePunct == '?')
{
if (remainder == 0) {
System.out.println("Yes");`
System.out.println("Please Input another sentence (terminate with \"STOP \")");
sentenceInput= myScanner.nextLine();
}
else {
System.out.println("No");
System.out.println("Please Input another sentence (terminate with \"STOP \")");
sentenceInput= myScanner.nextLine();
}
}
else if (sentencePunct == '!') {
System.out.println("Wow!");
System.out.println("Please Input another sentence (terminate with \"STOP \")");
sentenceInput= myScanner.nextLine();
}
else {
System.out.println("You always say " + sentenceInput);
System.out.println("Please Input another sentence (terminate with \"STOP \")");
sentenceInput= myScanner.nextLine();
}
}
}
2
solved Ttrying to loop until user types in “Stop”
Looping Until User Types “Stop”
Looping is a powerful programming tool that allows you to repeat a set of instructions until a certain condition is met. In this tutorial, we will learn how to create a loop that will continue until the user types in the word “Stop”.
Creating the Loop
We will use a while
loop to create our loop. The while
loop will continue to execute until the condition inside the parentheses is false. In this case, we want the loop to continue until the user types in the word “Stop”. To do this, we will use the input()
function to get the user’s input and store it in a variable. We will then check if the user’s input is equal to the word “Stop”. If it is, the loop will end. If not, the loop will continue.
while True:
user_input = input("Type something: ")
if user_input == "Stop":
break
In the code above, we create a while
loop that will continue until the user types in the word “Stop”. Inside the loop, we use the input()
function to get the user’s input and store it in the user_input
variable. We then check if the user’s input is equal to the word “Stop”. If it is, we use the break
statement to end the loop. If not, the loop will continue.
Conclusion
In this tutorial, we learned how to create a loop that will continue until the user types in the word “Stop”. We used a while
loop and the input()
function to get the user’s input and store it in a variable. We then checked if the user’s input was equal to the word “Stop”. If it was, we used the break
statement to end the loop. This is a simple but powerful technique that can be used in many different situations.