帮忙写一个简单的python程序π_π 要求就是图上的那样 其实挺简单的但我就是写不出来。拜托了,
发布网友
发布时间:2022-04-07 11:10
我来回答
共2个回答
懂视网
时间:2022-04-07 15:32
python中的π就是math.pi变量 。它被包含在标准库中, 在你试图自己计算它之前,应该先去导入math库,才能调用math.pi方法 。
热心网友
时间:2022-04-07 12:40
要求提的不是很明确,按照最简单的功能来做了。
只记录姓名和电话号码,没有做号码检验。也没有做保存功能。
用的python2.7. 有额外需求请私聊。
#!/usr/bin/env python
import sys
last_id = 0
class Person:
def __init__(self, name, phone):
self.name = name
self.phone = phone
global last_id
last_id += 1
self.id = last_id
def match(self, filter):
return filter in self.name or filter in self.phone
class AddressBook:
def __init__(self):
self.people = []
def new_person(self, name, phone=''):
self.people.append(Person(name, phone))
def find_person(self, id):
for person in self.people:
if str(id) == str(person.id):
return person
return None
def modify_person(self, id, name, phone):
person = self.find_person(id)
if person:
person.name = name
person.phone = phone
else:
print "person:", id, " not found"
def delete_person(self, id):
person = self.find_person(id)
if person:
self.people.remove(person)
def search(self, filter):
return [person for person in self.people if person.match(filter)]
class Menu:
def __init__(self):
self.address_book = AddressBook()
self.choices = {
"1": self.show_people,
"2": self.add_person,
"3": self.remove_person,
"4": self.modify_person,
"5": self.search_person,
"6": self.quit
}
def display_menu(self):
print("""
Address Book Menu
1. Show all People
2. Add Person
3. Remove Person
4. Modify Person
5. Search Person
6. Quit
""")
def run(self):
while True:
self.display_menu()
choice = raw_input("Enter an option: ")
action = self.choices.get(choice)
if action:
action()
else:
print("{0} is not a valid choice".format(choice))
def show_people(self, people=None):
if not people:
people = self.address_book.people
for person in people:
print("{0}: {1} {2}".format(person.id, person.name, person.phone))
def search_person(self):
filter = raw_input("search for: ")
people = self.address_book.search(filter)
self.show_people(people)
def add_person(self):
name = raw_input("Enter a name: ")
phone = raw_input("Enter his/her phone number: ")
self.address_book.new_person(name, phone)
print("{0} has been added.".format(name))
def remove_person(self):
id = raw_input("Enter his/her id: ")
if id:
self.address_book.delete_person(id)
def modify_person(self):
id = raw_input("Enter a person id: ")
name = raw_input("Enter his/her name: ")
phone = raw_input("Enter his/her phone: ")
self.address_book.modify_person(id, name, phone)
def quit(self):
print("Thank you for using your address book today.")
sys.exit(0)
Menu().run()
追问十分感谢!真的!