202k views
1 vote
g Write a function that collects integers from the user until a 0 is encountered and returns then in a list in the order they were input.

User GEkk
by
5.5k points

1 Answer

2 votes

Answer:

The function written in python is as follows;

def main():

user_input = int(input("Input: "))

mylist = []

while not user_input == 0:

mylist.append(user_input)

user_input = int(input("Input: "))

print(mylist)

if __name__ == "__main__":

main()

Step-by-step explanation:

This line declares a main function with no arguments

def main():

This line prompts user for input

user_input = int(input("Input: "))

This line declares an empty list

mylist = []

The following iteration checks if user input is not 0; If yes, the user input is appended to the list; otherwise, the iteration ends

while not user_input == 0:

mylist.append(user_input)

user_input = int(input("Input: "))

This liine prints the list in the order which it was inputted

print(mylist)

The following lines call the main function

if __name__ == "__main__":

main()

User Bill Turner
by
5.0k points