This program should use a main function and two other functions named playlist and savelist as follows: The main function: The main function should create an empty list named nums and then use a loop to add ten integers to nums, each integer in the range from 10-90. NOTE: In Python, a list is a data type. See Chapter 7. The main function should then call the playlist function and savelist function, in that order. Both of these functions take the nums list as a sole argument. The playlist function: playlist should use a loop to display the elements of the list, all on one line separated by a single space. playlist should also determine and print the total of the even numbers and the total of the odd numbers. The savelist function: save list should sort the list and write each element to a text file named angles.txt, each element on its own line.
SAMPLE OUTPUT
Here are the integers in nums:
23 85 31 55 42 85 72 33 39 20
Even integers total to 134
Odd integers total to 351
List of integers was written to file.
End of program.
Here is the code for you:
#!/usr/bin/python
def playlist(nums):
# playlist should use a loop to display the elements
of the list, all on one line
# separated by a single space
for num in nums:
print num,
print
# playlist should also determine and print the total
of the even numbers and the total
# of the odd numbers.
evens = 0
odds = 0
for num in nums:
if num % 2 == 0:
evens +=
num
else:
odds +=
num
print 'Even integers total to ', evens
print 'Odd integers total to ', odds
def savelist(nums):
# save list should sort the list and write each
element to a text file named angles.txt,
# each element on its own line.
nums.sort()
outfile = open('angles.txt', 'w')
for num in nums:
outfile.write(str(num) +
'\n')
print 'List of integers was written to
file.'
def main():
# The main function should create an empty list named
nums and then use a loop to add
# ten integers to nums, each integer in the range from
10-90.
nums = []
for i in range(10):
print 'Enter integer #', (i+1), '
(in range 10 - 90): ',
val = input()
nums.append(val)
# The main function should then call the playlist
function and savelist function, in that order.
playlist(nums)
savelist(nums)
main()
And the output screenshot is:
This program should use a main function and two other functions named playlist and savelist as...
Problem: Create a program that contains two functions: main and a void function to read data from a file and process it. Your main function should prompt the user to enter the name of the file (Lab7.txt), store this name, and then call the function sending the name of the file to the function. After the function has completed, the main function should output the total number of values in the file, how many of the values were odd, how...
Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function calls. Be sure to read in data from the input file. Using the input file provided, run your program to generate an output file. Upload the output file your program generates. •Write a...
FUNCTIONS In this assignment, you will revisit reading data from a file, and use that data as arguments (parameters) for a number of functions you will write. You will need to: Write and test a function square_each(nums) Where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, a return will not be allowed! Write and test a function sum_list(nums) Where nums is a...
Write a Python named numbersread.py that uses a loop to read and process numbers.txt. The program should output all of the integers, one per line, and then output the total of the even integers and the total of the odd integers.
Write a program that will do the following. The main function should ask the user how many numbers it will read in. It will then create an array of that size. Next, it will call a function that will read in the numbers and fill the array (fillArray). Pass the array that was made in themain function as a parameter to this function. Use a loop that will read numbers from the keyboard and store them in the array. This...
Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...
Java Programming Write a method named isEven that accepts an integer argument. The method should return true if the argument is even, or false if not. The program’s main method should use a loop to generate a random integer. The number of random integers is provided by a user, who must enter a number greater than 10. The loop should then invoke the isEven method to determine whether each integer is even or odd, display the random number and the...
Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all .cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printOdd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention to the order of your function...
Please Use C++ for coding . . Note: The order that these functions are listed, do not reflect the order that they should be called. Your program must be fully functional. Submit all.cpp, input and output files for grading. Write a complete program that uses the functions listed below. Except for the printodd function, main should print the results after each function call to a file. Be sure to declare all necessary variables to properly call each function. Pay attention...
In this assignment, you will revisit reading data from a file, and use that data as arguments (parameters) for a number of functions you will write. You will need to: Write and test a function square_each(nums) Where nums is a (Python) list of numbers. It modifies the list nums by squaring each entry and replacing its original value. You must modify the parameter, return will not be allowed! Write and test a function sum_list(nums) Where nums is a (Python) list...