Python嵌套IF

Python嵌套IF,python,if-statement,logic,Python,If Statement,Logic,和这里的许多人一样,我对Python还是新手。我正在编写一个代码段,要求用户提供他们的ID,然后检查ID的长度是否正好是6位数字。然后,代码将要求用户确认他们的ID,如果他们输入错误,允许他们重置ID。如果用户确认他们的输入是正确的,那么它会请求一个位置ID并遵循相同的路径。如果两个ID都已确认,则用户可以继续执行项目的其余部分 这是必须在每次使用开始时输入的内容 我现在讨论的问题有三个方面 1.)我可以输入empID 101290,有时它会告诉我这是一个有效的条目,而其他人则不会(但10125

和这里的许多人一样,我对Python还是新手。我正在编写一个代码段,要求用户提供他们的ID,然后检查ID的长度是否正好是6位数字。然后,代码将要求用户确认他们的ID,如果他们输入错误,允许他们重置ID。如果用户确认他们的输入是正确的,那么它会请求一个位置ID并遵循相同的路径。如果两个ID都已确认,则用户可以继续执行项目的其余部分

这是必须在每次使用开始时输入的内容

我现在讨论的问题有三个方面

1.)我可以输入empID 101290,有时它会告诉我这是一个有效的条目,而其他人则不会(但101256无论如何都有效-都是6位数字)

2.)输入“1”以确认ID,代码移到第2块并要求输入位置ID,但如果用户输入“2”表示员工ID错误,代码仍会继续

这里有什么需要改变的建议吗

import time
print('What is your employee ID?') #user assigned ID
empID = input()
while empID != 0:
    print('Try again.')
    empID = input()

# employee ID is only 6 digits in length, no letters
    if len(empID) != 6:
        print('Try again.')
    elif len(empID) == 6:
        print('Thank you. Your ID is set to ' + empID + '.')
        time.sleep(.5)
        print('Is this correct?'''
              '[1] Yes  [2] No ')
        yesNo = input()
        while True:
            yesNo == '1'
            print('Thank you. ID set.')
            break
# reset ID
        else:
            print('ID has been reset. Please enter your employee ID.')
            empID = input()
            break
    break

#Store Location ID
print('What is your Location ID?')
locID = input()
while locID != 0:
    print('Try again.')
    locID = input()

# store locations are 3-5 digits
# TODO: prepend any input with less than len 5 with 0
    if len(locID) != 5:
        print('Try again.')
    elif len(locID) == 5:
        print('Thank you. Your location is set to ' + locID + '.')
        time.sleep(.5)
        print('Is this correct?'''
              '[1] Yes  [2] No ')
        yesNo = input()
        while True:
            yesNo == '1'
            print('Thank you. Location ' + locID + 'set.')
            break
        else:
            print('Location ID has been reset. Please enter your location code.')
            empID = input()
            break
        break
    break
#next

首先,我看到您的代码中有一些bug

while True:
    yesNo == '1' 
yesNo==“1”
是一个条件语句,根据用户输入返回true或false,但它不在任何地方用作条件

if len(empID) != 6:
    print('Try again.')
elif len(empID) == 6: 

`elif len(empID) == 6:` is redundant.. a simple else will do
我要做的是: 定义验证用户凭据的函数:

def isEmpID(id):
    '''
    Employee ID is 6 characters in Length
    '''
    if len(id) != 6:
        return False
    return True


def isStoreID(id):
    '''
    Store ID is 3-6 characters in Length
    Note: The function when called with id, checks if the length is between (exclusive) 3 and (inclusive) 6 and returns true if condition is satisfied else false which is the default return policy
    '''
    if 3 < len(id) <= 6:
        return True
    return False


validEmpID = False
validStoreID = False
while not (validEmpID and validStoreID): # Both has to be True to exit the loop, Otherwise the condition continues to go to True.
    if not validEmpID:
        print('Enter Employee ID:')
        empID = input()
        validEmpID = isEmpID(empID)
        if not validEmpID:
            print('Invalid Employee ID\nTry Again...\n')
            continue
    print('Enter Store ID:')
    strID = input()
    validStoreID = isStoreID(strID)
    if not validStoreID:
        print("Invalid Store ID\nTry Again!...\n")
        continue
def isEmpID(id):
'''
员工ID的长度为6个字符
'''
如果len(id)!=6:
返回错误
返回真值
def isStoreID(id):
'''
存储ID的长度为3-6个字符
注意:当使用id调用该函数时,将检查长度是否介于(独占)3和(包含)6之间,如果满足条件则返回true,否则返回false,这是默认的返回策略
'''

如果3
while True:
    yesNo == '1' 
yesNo==“1”
是一个条件语句,根据用户输入返回true或false,但它不在任何地方用作条件

if len(empID) != 6:
    print('Try again.')
elif len(empID) == 6: 

`elif len(empID) == 6:` is redundant.. a simple else will do
我要做的是: 定义验证用户凭据的函数:

def isEmpID(id):
    '''
    Employee ID is 6 characters in Length
    '''
    if len(id) != 6:
        return False
    return True


def isStoreID(id):
    '''
    Store ID is 3-6 characters in Length
    Note: The function when called with id, checks if the length is between (exclusive) 3 and (inclusive) 6 and returns true if condition is satisfied else false which is the default return policy
    '''
    if 3 < len(id) <= 6:
        return True
    return False


validEmpID = False
validStoreID = False
while not (validEmpID and validStoreID): # Both has to be True to exit the loop, Otherwise the condition continues to go to True.
    if not validEmpID:
        print('Enter Employee ID:')
        empID = input()
        validEmpID = isEmpID(empID)
        if not validEmpID:
            print('Invalid Employee ID\nTry Again...\n')
            continue
    print('Enter Store ID:')
    strID = input()
    validStoreID = isStoreID(strID)
    if not validStoreID:
        print("Invalid Store ID\nTry Again!...\n")
        continue
def isEmpID(id):
'''
员工ID的长度为6个字符
'''
如果len(id)!=6:
返回错误
返回真值
def isStoreID(id):
'''
存储ID的长度为3-6个字符
注意:当使用id调用该函数时,将检查长度是否介于(独占)3和(包含)6之间,如果满足条件则返回true,否则返回false,这是默认的返回策略
'''

如果3if
循环不存在。缩进错误,问题中的代码语法无效。除非你把它修好,否则我们帮不了你。编辑您的问题,再次将代码复制到问题中,选择所有内容,然后单击大括号按钮将其缩进(从而将其格式化为代码块)。好的,然后我将其称为错误。谢谢Alex,我想我已修复了格式。抱歉。乍一看,我猜这不是while True,而是:yesNo=='1',如果yesNo=='1',并且没有break语句,那么您想要的是。但是,我还没有测试这是否解决了您列出的问题。
如果循环不存在。您的缩进错误,问题中的代码语法无效。除非你把它修好,否则我们帮不了你。编辑您的问题,再次将代码复制到问题中,选择所有内容,然后单击大括号按钮将其缩进(从而将其格式化为代码块)。好的,然后我将其称为错误。谢谢Alex,我想我已修复了格式。抱歉。乍一看,我猜这不是while True,而是:yesNo=='1',如果yesNo=='1',并且没有break语句,那么您想要的是。但是,我还没有测试这是否解决了您列出的问题。我感谢您的洞察力和更正。它将继续挂起并给我一个语法错误,除非我在
def
块之间删除
next
,并且当我将
elif
更改为建议的
else
时,它希望缩进
next()
是用于迭代器的函数。代码中的任何地方都不需要它。另外,
else
仅在
时与
一起使用,而在
时不与
一起使用。请在上面张贴您编辑的代码。让我看看,好了。我真的很感谢你看。我不会在几个小时内做更多的事情——必须让孩子们上床睡觉,还有所有那些有趣的爸爸的事情:)我很感激这些洞察和纠正。它将继续挂起并给我一个语法错误,除非我在
def
块之间删除
next
,并且当我将
elif
更改为建议的
else
时,它希望缩进
next()
是用于迭代器的函数。代码中的任何地方都不需要它。另外,
else
仅在
时与
一起使用,而在
时不与
一起使用。请在上面张贴您编辑的代码。让我看看,好了。我真的很感谢你看。我在几个小时内不会再做更多的事情了——必须让孩子们上床睡觉,还有所有那些有趣的爸爸的事情:)