gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
Write a program that gets input from user until user enters -1, and display the…
- User
Write a program that gets input from user until user enters -1, and display the output.Use for loop.
- Assistantpython
numbers = [] while True: num = int(input("Enter a number (-1 to exit): ")) if num == -1: break numbers.append(num) print("Numbers entered:") for num in numbers: print(num)Explanation:
- Initialize an empty list
numbersto store the numbers entered by the user. - Use a while loop with a condition
Trueto continuously take input from the user. - Prompt the user to enter a number and convert it to an integer using the
int()function. - Check if the entered number is equal to -1. If so, break out of the loop and move to the next step.
- If the number is not -1, append it to the
numberslist. - After the loop ends, print the message "Numbers entered:".
- Use a for loop to iterate over the elements in the
numberslist. - Within the loop, print each number.
- The program will keep asking the user for input until the user enters -1. After that, it will display all the numbers entered by the user.
- Initialize an empty list