Python 3.0 1. program_p1.py This program should use a main function and two othe
ID: 672256 • Letter: P
Question
Python 3.0
1. program_p1.py
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 random integers to nums, each integer in the range from 10-90. 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.
2. program_p2.py
This program should consider that the integers in angles.txt represent angles measured in degrees. The program should read angles.txt and generate a table showing the trigonometric ratios sine, cosine, and tangent of the angles. The ratios should be displayed in formatted columns accurate to 5 decimal places as shown in the sample output below.
SAMPLE OUTPUT
Angle Sine Cosine Tangent
20 0.34202 0.93969 0.36397
23 0.39073 0.92050 0.42447
31 0.51504 0.85717 0.60086
33 0.54464 0.83867 0.64941
39 0.62932 0.77715 0.80978
42 0.66913 0.74314 0.90040
55 0.81915 0.57358 1.42815
72 0.95106 0.30902 3.07768
85 0.99619 0.08716 11.43005
85 0.99619 0.08716 11.43005
End of program.
Explanation / Answer
program_p1.py
-------------
def main():
l = []
while len(l) < 10:
x = int(input("Enter a number: "))
if x >= 10 and x <= 90: l.append(x)
else: print("Invalid range")
playlist(l)
savelist(l)
print("End of Program A.")
def savelist(l):
l = sorted(l)
f = open('angles.txt', 'w+')
for x in l:
f.write("%d " % x)
f.close()
def playlist(l):
for x in l:
print(" ",x,end='' )
print("Here are the angles in degrees: "),
num1 = int(input('Enter a number: '))
num2 = int(input('Enter a number: '))
num3 = int(input('Enter a number: '))
num4 = int(input('Enter a number: '))
num5 = int(input('Enter a number: '))
num6 = int(input('Enter a number: '))
num7 = int(input('Enter a number: '))
num8 = int(input('Enter a number: '))
num9 = int(input('Enter a number: '))
num10 = int(input('Enter a number: '))
for num in range(num1, num10):
if num%2==0:
print( num," are even numbers")
continue
print(num, " are odd numbers")
sum = 0;
for i in range(10, 91):
if(i%2)!= 0:
sum = sum + i;
print("sum of odd numbers", sum);
for i in range(10, 91):
if i%2 == 0:
sum = sum + 1;
print("sum of even numbers", sum);
program_p2.py
-------------
import math
print 'Degrees Radians Sine Cosine Tangent'
print '------- ------- ------- -------- -------'
fmt = ' '.join(['%7.2f'] * 5)
for deg in range(0, 361, 30):
rad = math.radians(deg)
if deg in (90, 270):
t = float('inf')
else:
t = math.tan(rad)
print fmt % (deg, rad, math.sin(rad), math.cos(rad), t)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.