为什么我的Validation()函数没有运行?

2024-06-15 00:52:42 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我写的代码,在考虑年龄和选举类型后,尝试显示某人是否能够投票的消息,但在输入选举类型后,它将停止。 有人能帮忙吗? 这是我的密码:

def Validation():
if election == 'EU' or election == 'UK':
    if age < 110 and age > 18:
        print('You are eligable to vote in this election')
    else:
        print('You are not eligable to vote in this election')

    if election is 'Scottish' or election is'local':
        if age < 110 and age > 16:
            print('You are eligable to vote in this election')
        else:
            print('You are not eligable to vote in this election')

Tags: orandtoinyou类型ageif
3条回答

免责声明:以下不是我通常做的事情,也不是本网站通常发生的事情。它远远超出了问题的范围,几乎可以归结为101或教程。你知道吗


您的代码有多个问题,并且没有一个问题可以通过发现错误并指出错误来轻松修复。整个解决问题的方法需要从头开始重新研究。您试图一次做很多事情,这总是导致代码出现互锁问题。你知道吗

让我们把你想做的事情分解一下。你知道吗

  1. 询问用户的年龄,必须是0到110之间的整数。你知道吗
  2. 询问他们的地区,这必须是有限的选择之一
  3. 告知他们的年龄是否允许他们在本地区投票

更基本的是,你想这样做:

  1. 向用户请求值
  2. 确保该值满足某个标准(如“是一个数字”“是一个介于x和y之间的数字”
  3. 重复此操作,直到用户输入了符合要求的值

这些步骤可以用伪代码表示:

def input_value():
    while True:
        value = input('Please enter a value: ')
        if value == 'valid':
            return value

因此,我们有一个无休止的循环,不断地要求用户提供一个值,只有当值正常时,我们才会返回它。你知道吗


要请求整数,函数可以如下所示:

def input_int():
    while True:
        value = input('Please enter a number: ')
        if value.isdigit():
            return int(value)

要请求一个属于某个范围的整数,比如年龄值,我们可以使用相同的模式并使用input_int()函数:

def input_int_in_range(min, max):
    while True:
        value = input_int()
        if value >= min and value <= max:
            return value
        else:
            print('Please input values between %i and %i.' % (min, max))

注意else:分支如何在第一个错误条目之后通知用户有效范围。你知道吗

现在假设我们有dict的投票规则,如我的另一个答案所示:

regions = {
    '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},
}

我们可以编写另一个函数,让用户通过键选择其中一个。函数很好地遵循既定模式:

def input_choose(dict):
    while True:
        value = input('Please input your choice: ')
        if value in dict:
            return dict[value]
        else:
            print('Valid choices are: %s'  % ', '.join(dict.keys()))

现在,我们已经准备好了所有基本部件,可以将它们组合在一起:

# main program
while True:
    print("\nPlease enter your age.")
    age = input_int_in_range(0, 110)

    print("\nPlease enter the election you are voting for.")
    rule = input_choose(rules)

    if age >= rule['min-age'] and age <= rule['max-age']:
        print("\nYou are eligible to vote in this election.\n")
    else:
        print("\nYou are not eligible to vote in this election.\n")

作为一个练习,您可以创建一个验证函数,但是每个输入步骤的设计方式都是自验证的,因此最终的验证步骤实际上是不必要的。你知道吗

==代替is。还要注意==应该匹配每个字符,这样才是真的(这有点奇怪,但请记住这一点),因此,如果您有一个名为“Local”的选择,并且您正试图与“Local”进行比较,那就不一样了。你知道吗

您的代码应该如下所示:

def Validation():
    if election == 'EU' or election == 'UK':
        if age < 110 and age > 18:
            print('You are eligable to vote in this election')
        else:
            print('You are not eligable to vote in this election')

        if election == 'Scottish' or election == 'local':
            if age < 110 and age > 16:
                print('You are eligable to vote in this election')
            else:
                print('You are not eligable to vote in this election')

除了@Fusseldieb编写的内容之外,还要考虑将配置与验证函数分离。你知道吗

规则可以这样表达:

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},
}

这样一来,验证函数就变短了,而且与当前的方法相比,如果规则集变大,它不需要任何更改。你知道吗

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')

相关问题 更多 >