gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
Using knowledge of loops, if statements, compound conditions, strings, lists, and binary…
- User
Using knowledge of loops, if statements, compound conditions, strings, lists, and binary numbers "Question 2. (3 marks correctness, 3 marks demo, 1 mark documentation) Write a program that takes a list of numbers as input from the user, prints the list of numbers that the user entered, and prints the sum of the even and odd numbers in the list. Instructions:
- Declare an empty list to store the numbers.
- Prompt the user to enter a series of numbers, one at a time, until the input “done” is given.
- Append each number to the list.
- Calculate the sum of the even and odd numbers in the list separately. Note: In this case the program is looking for even and odd numbers given by the user, not odd and even indices.
- Print the list of numbers to the screen.
- Print the sum of the even numbers.
- Print the sum of the odd numbers
- Assistant
.
Documentation:
This program takes input from the user in the form of numbers and stores them in a list. It then calculates the sum of the even and odd numbers separately and prints them along with the list of numbers entered by the user.
Demo:
Enter a number (type "done" to finish): 5 Enter a number (type "done" to finish): 8 Enter a number (type "done" to finish): 3 Enter a number (type "done" to finish): 12 Enter a number (type "done" to finish): done List of numbers: [5, 8, 3, 12] Sum of even numbers: 20 Sum of odd numbers: 8Code:
# declare an empty list to store numbers numbers = [] # prompt user to enter numbers until "done" is given while True: num_input = input("Enter a number (type \"done\" to finish): ") if num_input == "done": break else: numbers.append(int(num_input)) # initialize variables for sum of even and odd numbers sum_even = 0 sum_odd = 0 # calculate sum of even and odd numbers in the list for num in numbers: if num % 2 == 0: sum_even += num else: sum_odd += num # print list of numbers and sum of even and odd numbers print("List of numbers:", numbers) print("Sum of even numbers:", sum_even) print("Sum of odd numbers:", sum_odd)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.