作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<分区>
我正在研究 OOP,并制作了一个命令行待办事项列表以供练习。我测试了 Menu 中使用的类中的所有方法,但无法找到一种方法来测试我的类 Menu。
在 Menu 类中,方法要求用户键入输入,但我不知道如何测试它。
我没有在这里发布我的所有代码,因为我只是将它用作我的问题的示例,因为我想要一个更通用的答案。
import sys
from todo.task import TaskContainer
class Menu:
def __init__(self):
self.tasks = TaskContainer()
self.choices = {
1 : self.create_new_task,
2 : self.edit_task,
3 : self.edit_task_description,
4 : self.edit_task_status,
5 : self.search_task_by_id,
6 : self.search_task_by_word,
7 : self.delete_task,
8 : self.show_all_tasks,
9 : self.show_task_description,
10 : self.show_task_date,
11 : self.quit}
def display_menu(self):
print("""
1 : Create new task
2 : Edit task
3 : Edit task description
4 : Edit task status
5 : Search task by id
6 : Search task by matching word
7 : Delete task by id
8 : Show all tasks
9 : Show task description
10 : Show task dates
11 : Quit
""")
def run(self):
while True:
self.display_menu()
choice = int(input("Enter an option: "))
action = self.choices.get(choice)
if action:
action()
else:
print("{} is not a valid choice.".format(choice))
def create_new_task(self):
task = input("Task: ")
due_date = input("What is the due date for this task? ")
description = input("If you wish, add a description to this task:")
self.tasks.new_task(task)
def edit_task(self):
id = int(input("Type the ID of the task you wish to edit: "))
changed_task = input("New task: ")
self.tasks.edit_task(id, changed_task)
def edit_task_description(self):
id = int(input("Type the id of the task you want to edit the description? "))
description = input("Type description: ")
self.tasks.edit_task_description(id, description)
def edit_task_status(self):
id = int(input("Type the ID of the task you wish to change status: "))
status = input("Status: ")
self.tasks.edit_task_status(id, status)
def search_task_by_id(self):
id = int(input("Type task ID: "))
print(self.tasks.search(id))
def search_task_by_word(self):
word = input("Type the word to be matched: ")
matches = self.tasks.search_word(word)
[print(task) for task in matches]
def delete_task(self):
id = int(input("Type the ID of the task you wish to delete: "))
self.tasks.delete_task(id)
def show_all_tasks(self):
self.tasks.show_all_tasks()
def show_task_description(self):
id = int(input("Type the ID you wish to see the description: "))
print(self.tasks.show_description(id))
def show_task_date(self):
id = int(input("Type the ID you wish to see the due date: "))
print(self.tasks.show_due_date(id))
def quit(self):
sys.exit(0)
if __name__ == "__main__":
Menu().run()
此时,程序显示两种选择并打印一条语句,表明用户赢了、计算机赢了还是平局。继续玩直到用户选择停止,然后打印用户赢的次数,损失, 和关系. import java.util.Scanner; import
我想在 d3.js 中制作一个条形图,每个项目或行都有正条和负条,如下所示: 它有点像谷歌金融“行业摘要”图表(http://google.com/finance) 任何人都可以指出我这种图表的 d3
尝试根据 GF 和 GA 创建一个新列“Results”。想知道如何通过定义函数和条件语句来做到这一点。以下是我对一行的初步尝试,但无法弄清楚如何将其应用于所有行,非常感谢您的帮助! 日期地点对手GF
我对 Java 还很陌生,并且有一项任务被困住了。我相信我的 boolean 函数是正确的,但是我不知道在主函数中要写什么。 这是作业: 编写一个公共(public)函数(静态方法)winner(in
使用掷骰子游戏中的 10,000 次模拟来计算获胜概率 (wins/(wins + Loss))。下面是双骰子游戏的方法: public class CrapsGame { public st
我是一名优秀的程序员,十分优秀!