【发布时间】: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