【问题标题】:Python - Nested IFPython - 嵌套 IF
【发布时间】:2016-12-26 23:17:29
【问题描述】:

和这里的许多人一样,我是 Python 新手。我正在开发一个 sn-p,它要求用户提供他们的 ID,然后检查 ID 的长度是否正好是 6 位数字。然后代码将要求用户确认他们的 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

【问题讨论】:

  • if 循环不存在。
  • 您的缩进错误,问题中的代码不是有效的语法。除非你修复它,否则我们无能为力。编辑您的问题,再次将代码复制到问题中,全选,然后单击花括号按钮将其缩进(从而将其格式化为代码块)。
  • 好吧,那我叫错了。
  • 谢谢亚历克斯,我想我修正了格式。对不起。
  • 一目了然,我猜不是 while True: yesNo == '1' 你想要 if yesNo == '1',并且没有 break 语句。但是,我尚未测试这是否能解决您列出的问题。

标签: python if-statement logic


【解决方案1】:

我在您的代码中发现了一些错误。

while True:
    yesNo == '1' 

yesNo == '1' 是一个条件语句,根据用户输入返回真或假,但它不用作任何地方的条件

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 块之间删除 next 并且当我将 elif 更改为建议的 else 时它需要缩进。
  • next() 是一个用于迭代器的函数。您在代码中的任何地方都不需要它。此外,else 仅与 if 一起使用,而不与 while 一起使用。
  • 请在上面发布您编辑的代码。让我看看吧
  • 完成。我很欣赏它。几个小时内我不会用它做更多的事情 - 必须让孩子上床睡觉和所有有趣的爸爸的东西:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-28
  • 2016-08-30
  • 1970-01-01
  • 2015-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多