In: Computer Science
1. Adhere to the naming conventions discussed in class for variable names, program names, and function names
2. Use meaningful names for variables
3. Follow the coding standards and homework submission rules as covered in class.
4. If there are more than one word in the variable use all lowercase and separate them with underscores, e.g., first_name, shipping_cost.
5. Include appropriate comments in the code, but not too many.
6. Output should be displayed exactly as shown for each problem below. The output given is sample output. Other numbers/input could be entered.
7. If your program does not compile, you will not receive more than 20% of the maximum points.
8. If your program compiles but does not execute, you will not receive more than 35% of the maximum points.
9. If your program compiles and executes yet does not give the required output, you will not receive more than 50% of the maximum points.
10. The programs will be in one file named hw3.py. The module will be in one file named number_functions.py. Zip hw3.py and number_functions.py into file hw3.zip and upload to Canvas. Only .zip files will be accepted. If the file is not named correctly, 10 points will be deducted.
11. Each program will be separated by a comment with the problem number. If this format is not followed, 10 points will be deducted.
12. Your name will be a comment at the top of the file. If your name is not in the file as a comment, 10 points will be deducted.
13. If you use Python constructs on assignments that we have not yet discussed in class, 20 points will be deducted.
14. If you do not adhere to our coding standards, 20 points will be deducted.
#Problem 2
Write function write_stock that asks the user to input a company name, its associated ticker symbol (eg. Exxon, XOM), and the price per share of the stock. These are then written to file stock.txt one item per line. The user will type in ‘quit’ to stop.
Write function get_share_value that accepts a company name and the number of shares. The function will search file stock.txt and return a list of two values; the ticker symbol and the total value of the shares. This function must use an exception handler to insure the file is valid, that the price can be converted to a number, and catch any other exception that might be raised.
Write a program that calls write_stock and get_share_value. The program will ask the user for the company name and number of shares for read_stock and print the answer.
Sample output might look as follows.
Enter company name; enter quit to stop: Amazon
Enter ticker symbol: amzn
Enter price: 1000
Enter company name; enter quit to stop: Apple
Enter ticker symbol: aapl
Enter price: 125
Enter company name; enter quit to stop: Microsoft
Enter ticker symbol: msft
Enter price: 250
Enter company name; enter quit to stop: quit
What stock did you buy?: Apple
How many shares?: 10
Your total value for aapl is $1250.00
#Problem 3
Write function determine_ sums that accepts a list of values. The function will return a list of two numbers; the first is the sum of the positive numbers and the second is the sum of the negative numbers.
Write a program that defines a list as [1, 5, 7, -2, 6, -8, 10, -4, -20] and calls determine_sums. The program will print the output. Assume the company name will start with a capital letter and the ticker symbol will be all lowercase. This will give the following output.
[29, -34]
There is no user input on this problem.
#Problem 4
Write function write_students that asks the user for student records and writes them to file students.txt. A record of a student includes name, major and GPA. The user can enter as many records as they want until they type in quit.
Write function read_students that accepts a major. The function returns the highest GPA for that major.
Write a program that calls write_students and read_students and prints the major and highest GPA. The program will ask the user for the major for which to search. Assume all names and majors will be lower case.
Sample input/output might look as follows:
Enter student name; enter quit to stop: jones
Enter major: insy
Enter gpa: 2.0
Enter student name; enter quit to stop: smith
Enter major: insy
Enter gpa: 3.25
Enter student name; enter quit to stop: willis
Enter major: mana
Enter gpa: 3.25
Enter student name; enter quit to stop: quit
Enter major: insy
The highest GPA for insy majors is 3.25
#Problem 5
Write module number_functions that includes:
Function write_numbers that writes 50 random numbers between 1 and 15 to file numbers.txt.
Function read_numbers that reads the file numbers.txt and returns a list of the numbers.
Write function count_numbers that accepts the list of numbers and returns how many numbers are in the list.
Function find_even_odd that accepts the list of numbers and returns a list of two numbers; how many odd numbers are in the list and how many even numbers are in the list.
Write a program that calls these functions in the order above and prints the returned data.
Sample output might be the following. But yours may vary as we are generating random numbers. The count will always be 50, but you need to code the function as if you do not know that.
['12', '8', '3', '18', '1', '5', '3', '18', '6', '10', '20', '8', '7', '4', '17', '16', '8', '11', '14', '3', '13', '13', '5', '17', '11', '9', '16', '19', '13', '18', '6', '1', '9', '19', '5', '14', '13', '15', '10', '12', '20', '16', '14', '5', '10', '20', '17', '11', '18', '3']
50
[26, 24]
# Problem 2
# function that asks the user to input a company name, its
associated ticker symbol and the price per share of the
stock.
# These are then written to file stock.txt one item per line. The
user will type in ‘quit’ to stop.
def write_stock():
# open file for writing
f = open("stock.txt", "w")
# loop to get inputs
while(1):
company_name =
input("Enter company name; enter quit to stop: ")
# if input is quit ,
exit loop
if(company_name=="quit"):
break
# else read remaining
data and write to file
ticker_symbol =
input("Enter ticker symbol: ")
stock_price =
float(input("Enter price: "))
f.write(company_name+"
")
f.write(ticker_symbol+"
")
f.write(str(stock_price)+"\n")
f.close()
# function that accepts a company name and the number of
shares.
# The function will search file stock.txt and
# return a list of two values; the ticker symbol and the total
value of the shares.
def get_share_value(company_name, share_count):
output_list = []
try:
f = open("stock.txt",
"r")
for x in f:
data = x.split(" ")
if(data[0]==company_name):
output_list.append(data[1])
value = float(data[2])*share_count
output_list.append(value)
return output_list
except:
print("Error occured
while reading file")
return output_list
#Problem 3
# function that accepts a list of values.
# return a list of two numbers; the first is the sum of the
positive numbers and the second is the sum of the negative
numbers
def determine_sums(list):
negative_sum = 0;
positive_sum = 0;
output_list = []
for number in list:
if number<0 :
negative_sum = negative_sum + number
elif number>0:
positive_sum = positive_sum + number
output_list.append(positive_sum)
output_list.append(negative_sum)
return output_list
write_stock()
company_name = input("What stock did you buy?: ")
share_count = int(input("How many shares?: "))
data = get_share_value(company_name, share_count)
if len(data)==0:
print("No such company found")
else:
print("Your total value for", data[0],"is ",
data[1],"\n")
number_list = [1, 5, 7, -2, 6, -8, 10, -4, -20]
sums = determine_sums(number_list)
print(sums)
screenshot of program for referencing indentation
OUTPUT
Completed Problem 2 and 3.
Post a new question for 4 and 5.