python9_1day

-- coding=UTF-8--

import json

username = input(“what is your name?”)
filename = ‘username.json’
with open(filename, ‘w’) as f_obj:
json.dump(username, f_obj)
print(“we’re rember you,when your back " + username + " .”)

读取数据

filename_1 = ‘username.json’
with open(filename_1) as f_obj_1:
username_1 = json.load(f_obj_1)
print(“we’ll come back " + username_1 + " !”)


测试代码

import unittest
from name_function import get_formatted_name

class NamesTestCase(unittest.TestCase):
“”“测试name_function”""

def test_first_last_name(self):
    """能正够确处理像Janis Joplin这样的姓名吗"""
    formatted_name = get_formatted_name('janis', 'joplin')
    self.assertEqual(formatted_name, 'Janis Joplin')

def test_first_last_middle_name(self):
    formatted_name = get_formatted_name('wolfgang', 'mozart', 'amadeus')
    self.assertEqual(formatted_name, 'Wolfgang Amadeus Mozart')

if name == ‘main’:
unittest.main()


from survey import AnonymousSurvey

定义一个问题,并创建一个表示调查的AnonymousSurvey

question = “what language did you the first learn to speak?”
my_survry = AnonymousSurvey(question)

显示问题并存储答案

my_survry.show_question()
print(“Enter ‘q’ at any time quit it !”)
while True:
response = input("Language: ")
if response == ‘q’:
break
my_survry.store_resption(response)

显示调查结果

print("\nThank you to everyone who parcited in the survey!")
my_survry.show_result()


测试AnonymousSurvey类

import unittest
from survey import AnonymousSurvey

class TestAnonymousSurvey(unittest.TestCase):
“”“针对AnonySurvey类的测试”""

def test_store_single_response(self):
    """测试单个答案将会被妥善的保存"""
    question = "what did you first learn to speak ?"
    my_survey = AnonymousSurvey(question)
    my_survey.store_resption('English')
    self.assertIn('English', my_survey.responses)

if name == ‘main’:
unittest.main()

猜你喜欢

转载自blog.csdn.net/qq_38501057/article/details/88426285
今日推荐