小编Bry*_*hor的帖子

Python 3单元测试用户输入

我绝对是Python单元测试的新手.我需要将它用于我必须提交的项目.我有点想知道从哪里开始,看起来我们基本上将测试参数放入我们在程序中定义的函数中,然后输入预期的结果.如果输出预期结果,我们就可以了,否则我们会得到失败或错误.

所以我的问题是我有多个用户输入存储在for循环或while循环中的变量中.我不知道从哪里开始为它们设置测试值.

这是我的所有代码:

studentTripExpenses = {}

def dictCreate(studentAmount):
    for i in range(0, studentAmount):
        studentName = input("What is the name of the student? ")
        expenseList = []
        print("Enter 'done' to move to the next student.")
        while True:
            expense = input("What is the cost of this expense? ")
            if expense.lower() == 'done':
                break
            elif (float(expense) >= 0) or (float(expense) < 0):
                expenseList.append(float(expense))
            elif not expense.isdigit():
                print("Please enter a number or enter 'done' to move on.")
        studentTripExpenses[studentName] = expenseList
    return studentTripExpenses

def studentCost(dct): …
Run Code Online (Sandbox Code Playgroud)

python unit-testing python-3.x

2
推荐指数
1
解决办法
1513
查看次数

使用IF,AND和OR语句不重复Excel中的代码

我试图找出在Excel中使用IF语句的最有效方法.我知道我希望我的代码看起来像是Python,但我在excel中苦苦挣扎.

下面是我在excel中的代码,它可以工作并做我想要的:

=IFS((AND(B4="Fall", C4="Triple")), 2190, (AND(B4="Fall", C4="Double")), 2731, (AND(B4="Fall", C4="Single")), 3175,
 AND(B4="Winter", C4="Triple"), 1902,  AND(B4="Winter", C4="Double"), 2372,  AND(B4="Winter", C4="Single"), 2757,
  AND(B4="Spring", C4="Triple"), 1671, AND(B4="Spring", C4="Double"), 2084, AND(B4="Spring", C4="Single"), 2423,
 ISBLANK(B4), "Pick A quarter", ISBLANK(C4), "Pick A dorm style")
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,有很多东西是重复的,没有必要.这使得阅读变得困难,并且很难打字.特别是因为我必须对AND()语句和结果中的第二个参数进行非常小的更改才能做同样的事情.

以下是我希望代码相当于的内容.

B4 = "Fall"
C4 = "Double"

if (B4=="Fall"):
    if (C4 == "Triple"):
        print(2190)
    if (C4 == "Double"):
        print(2731)
    if (C4 == "Single"):
        print(3175)
elif (B4 == "Winter"):
    if (C4 == "Triple"):
        print(1902)
    if (C4 == "Double"):
        print(2372)
    if (C4 == …
Run Code Online (Sandbox Code Playgroud)

python excel vba

1
推荐指数
1
解决办法
82
查看次数

标签 统计

python ×2

excel ×1

python-3.x ×1

unit-testing ×1

vba ×1