我正在重构一些旧代码,并且有以下循环:
def add_grade2(student, assignment_name, grade):
for s in student_list:
if s['first_name'] == student:
if (assignment_name, grade) in s['assignments']:
print(True)
elif (assignment_name, 0) in s['assignments']:
print('That assignment has a different grade. ')
break
elif (assignment_name, 1) in s['assignments']:
print('That assignment has a different grade. ')
break
elif (assignment_name, 2) in s['assignments']:
print('That assignment has a different grade. ')
break
elif (assignment_name, 3) in s['assignments']:
print('That assignment has a different grade. ')
break
elif (assignment_name, 4) in s['assignments']:
print('That assignment has a different grade. ')
break
else:
s['assignments'].append((assignment_name, grade))
print(False)
add_grade2('Alice', 'assignment_1', 3)
print(student_list)
我想将while (true)
{
if (stopLoop.IsStopRequested || ct.IsCancellationRequested)
{
break;
}
if (worker.IsPaused)
{
Thread.Sleep(delay);
continue;
}
if (wait)
{
Thread.Sleep(delay);
wait= false;
continue;
}
//much more code here...
}
和isPaused
合并为一个块,并正在考虑使用wait
。
在这种特殊情况下这有意义吗?这样的Func
循环会如何?
答案 0 :(得分:3)
可以用一个if
条件替换它吗?
if (wait || worker.IsPaused)
{
Thread.Sleep(delay);
wait = false;
continue;
}
如果wait
为true
,则始终将其设置为false
。如果是false
,则将其再次设置为false
。