Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么不是';我的Validation()函数是否正在运行?_Python_Function_Validation - Fatal编程技术网

Python 为什么不是';我的Validation()函数是否正在运行?

Python 为什么不是';我的Validation()函数是否正在运行?,python,function,validation,Python,Function,Validation,这是我写的代码,在考虑年龄和选举类型后,尝试显示某人是否能够投票的消息,但在我输入选举类型后,它将停止。 有人能帮忙吗? 这是我的密码: 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('Y

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

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')
def验证():
如果选举=‘欧盟’或选举=‘英国’:
如果年龄<110岁且年龄>18岁:
打印('您可以在此次选举中投票')
其他:
打印('您不能在此次选举中投票')
如果选举是“苏格兰的”或选举是“地方的”:
如果年龄<110岁且年龄>16岁:
打印('您可以在此次选举中投票')
其他:
打印('您不能在此次选举中投票')

Put
==
而不是
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')
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 input_int():
    while True:
        value = input('Please enter a number: ')
        if value.isdigit():
            return int(value)
def验证():
如果选举=‘欧盟’或选举=‘英国’:
如果年龄<110岁且年龄>18岁:
打印('您可以在此次选举中投票')
其他:
打印('您不能在此次选举中投票')
如果选举=‘苏格兰’或选举=‘本地’:
如果年龄<110岁且年龄>16岁:
打印('您可以在此次选举中投票')
其他:
打印('您不能在此次选举中投票')

除了@fuseldieb编写的内容外,还可以考虑将配置与验证功能分离

规则可以这样表示:

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')
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 input_int():
    while True:
        value = input('Please enter a number: ')
        if value.isdigit():
            return int(value)
这样一来,验证函数就变短了,与当前的方法相比,如果规则集变大,它就不需要任何更改

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')
def验证(选择):
如果按规则进行选举:
规则=规则[选举]

如果年龄>=规则['min-age']和年龄免责声明:以下不是我通常做的事情,也不是本网站通常发生的事情。它远远超出了问题的范围,几乎可以归结为101或教程


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

让我们把你想做的事情分解一下

  • 询问用户的年龄,年龄必须是0到110之间的整数
  • 询问他们所在的地区,这必须是有限的选择之一
  • 告知他们的年龄是否允许他们在所在地区投票
  • 更基本地说,您希望这样做:

  • 向用户询问一个值
  • 确保该值满足特定标准(如“是一个数字”或“是一个介于x和y之间的数字”)
  • 重复此操作,直到用户输入符合要求的值
  • 这些步骤可以用伪代码表示:

    def input_value():
        while True:
            value = input('Please enter a value: ')
            if value == 'valid':
                return value
    
    因此,我们有一个无休止的循环,不断地向用户询问一个值,只有当值正常时,我们才会返回它


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

    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')
    
    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 input_int():
        while True:
            value = input('Please enter a number: ')
            if value.isdigit():
                return int(value)
    
    要请求属于特定范围的整数,如年龄值,我们可以使用相同的模式并使用
    input\u 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))
    
    我们可以编写另一个函数,让用户按键选择其中一个。该函数很好地遵循了既定的模式:

    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")
    
    #主程序
    尽管如此:
    打印(“\n请输入您的年龄。”)
    年龄=输入范围(0,110)内的输入值
    打印(“\n请输入您要投票的选举。”)
    规则=输入\选择(规则)
    
    如果年龄>=规则['min-age']和年龄,
    if
    语句中的括号是多余的。这里没有歧义需要解决。@Tomalak但在某些语言中有多个语句,当它们没有分组时,代码不会被执行。这里不是这样<代码>如果A和B:
    等同于
    如果(A)和(B):
    。只有当偏好顺序不清楚时,才需要使用括号,如
    如果A和B或C:
    ,根据您的意图,括号可以是
    如果(A和B)或C:
    如果A和(B或C):
    。但是对于由
    /
    连接的条件的简单列表,不存在这样的不一致性。@Tomalak哦,这是有道理的。谢谢你向我澄清这一点。答案更新。这是一个非常好和干净的答案+我说:)我已经把所有这些都放进去了,但它一直得到的答案是“没有为这个国家配置任何规则”。原因很清楚。看看
    if
    语句,没有办法错误解释正在发生的事情。你是什么意思?我对Python还不熟悉,所以我仍在努力了解它。啊,现在我看到了您的其余代码,很清楚问题出在哪里了。下一次,请创建一个复制您的问题的函数,而不是只发布一个没有任何上下文的函数。真的,这将节省所有相关人员的大量时间。