擅长:python、mysql、java
<p>除了@Fusseldieb编写的内容之外,还要考虑将配置与验证函数分离。你知道吗</p>
<p>规则可以这样表达:</p>
<pre><code>rules = {
'UK': {'min-age': 18, 'max-age': 110},
'EU': {'min-age': 18, 'max-age': 110},
'Scottish': {'min-age': 16, 'max-age': 110},
'local': {'min-age': 16, 'max-age': 110},
}
</code></pre>
<p>这样一来,验证函数就变短了,而且与当前的方法相比,如果规则集变大,它不需要任何更改。你知道吗</p>
<pre><code>def Validation(election):
if election in rules:
rule = rules[election]
if age >= rule['min-age'] and age <= rule['max-age']:
print('You are eligible to vote in this election')
else:
print('You are not eligible to vote in this election')
else:
print('No rules configured for this country')
</code></pre>