Python Programmingの4週目
今週金曜日からまた旅行にいくので、課題をやるなら今日しかない。ということでやりました。
コースの構成は
1)
Lesson 1 - Long Strings, Random Library, Building and Sorting Lists
2)
Lesson 2 - Descriptive Statistics
3)
Lesson 3 - Formatting Print Statements
4)
Lesson 4 - Writing a Small Database Application
という形になっていて、
乱数、ソート、あとは統計 (statistics.meanとか、statistics.stdev都下の使い方)とか、簡単なデータベースアプリケーションとか。そういう課題。
統計関連の課題はこんな感じ
#%%
import random
numList = []
random.seed(150)
for i in range(0,25):
numList.append(round(100*random.random(),1))
#%%
def problem4_2(ran_list):
""" Compute the mean and standard deviation of a list of floats """
import statistics
print(statistics.mean(ran_list))
print(statistics.stdev(ran_list))
最後の簡単なデータベースアプリは、電話帳のプログラム。ほとんどできてるところに電話帳リストの並び替え機能を加えるのが課題だった。
初心者ようにヒントも書いてあるからちょっと考えればできたけど、2次元配列のソートとかどうやるのかなーと色々考えさせられる部分はあって、授業としてはよくできてるのかな。
電話帳アプリ。追加したのは、"r" を押したときのソート部分だけ。あとは最初から用意されていた。まあ何とかコース完了。投げ出さずに済んだのはよかったかな。また別のPythonのコースとか受けたり自分で何か遊んだりしてPythonをもっと使えるようにしておきたい。仕事でも役に立ちそうだし。
# -phones.py *- coding: utf-8 -*-
"""
This program maintains a database of names and phone numbers in a csv
file called myphones.csv. It is run from the command line and is menu
driven. To start it, save it in a directory and from the terminal run
>python phones.py
Version FINAL:
"""
import os
import csv
phones = []
name_pos = 0
phone_pos = 1
phone_header = [ 'Name', 'Phone Number']
def proper_menu_choice(which):
if not which.isdigit():
print ("'" + which + "' needs to be the number of a phone!")
return False
which = int(which)
if which < 1 or which > len(phones):
print ("'" + str(which) + "' needs to be the number of a phone!")
return False
return True
def delete_phone(which):
if not proper_menu_choice(which):
return
which = int(which)
del phones[which-1]
print( "Deleted phone #", which)
def edit_phone(which):
if not proper_menu_choice(which):
return
which = int(which)
phone = phones[which-1]
print("Enter the data for a new phone. Press <enter> to leave unchanged.")
print(phone[name_pos])
newname = input("Enter phone name to change or press return: ")
if newname == "":
newname = phone[name_pos]
print(phone[phone_pos])
newphone_num = input("Enter new phone number to change or press return: ")
if newphone_num == "":
newphone_num = phone[phone_pos]
phone = [newname, newphone_num]
phones[which-1] = phone
def save_phone_list():
f = open("myphones.csv", 'w', newline='')
for item in phones:
csv.writer(f).writerow(item)
f.close()
def load_phone_list():
if os.access("myphones.csv",os.F_OK):
f = open("myphones.csv")
for row in csv.reader(f):
phones.append(row)
f.close()
def show_phones():
show_phone(phone_header, "")
index = 1
for phone in phones:
show_phone(phone, index)
index = index + 1
print()
def show_phone(phone, index):
outputstr = "{0:>3} {1:<20} {2:>16}"
print(outputstr.format(index, phone[name_pos], phone[phone_pos]))
def create_phone():
print("Enter the data for a new phone:")
newname = input("Enter name: ")
newphone_num = input("Enter phone number: ")
phone = [newname,newphone_num]
phones.append(phone)
def menu_choice():
""" Find out what the user wants to do next. """
print("Choose one of the following options?")
print(" s) Show")
print(" n) New")
print(" d) Delete")
print(" e) Edit")
print(" r) Reorder")
print(" q) Quit")
choice = input("Choice: ")
if choice.lower() in ['n','d', 's','e','r', 'q']:
return choice.lower()
else:
print(choice +"?")
print("Invalid option")
return None
def reorder_phones():
global phones
phones.sort()
def main_loop():
load_phone_list()
while True:
choice = menu_choice()
if choice == None:
continue
if choice == 'q':
print( "Exiting...")
break # jump out of while loop
elif choice == 'n':
create_phone()
elif choice == 'd':
which = input("Which item do you want to delete? ")
print("which is ", which)
delete_phone(which)
elif choice == 's':
show_phones()
elif choice == 'e':
which = input("Which item do you want to edit? ")
print("which is ", which)
edit_phone(which)
elif choice == 'r':
reorder_phones()
else:
print("Invalid choice.")
save_phone_list()
# The following makes this program start running at main_loop()
# when executed as a stand-alone program.
if __name__ == '__main__':
main_loop()
コメント
コメントを投稿