提问人:Diego 提问时间:5/5/2023 最后编辑:ElkhanDiego 更新时间:5/5/2023 访问量:52
如何避免再次出现“输入您的姓名”消息?
how can I avoid the "Enter your name" message show up again?
问:
基本上,当调用employee_action函数时,用户按1或2并将数据打印到终端,用户按回车键后,再次显示“输入您的姓名”消息,并分配一个新的随机数。
def employee_action():
"""
Asks the user what they wish to do now that they're logged in.
"""
while True:
employee_input = input(
"What do you wish to do today?\n1 Get Personal Data \n2 Get Survey Responses\n")
if employee_input == "1":
data = worksheet.get_all_values()
for row in data:
print(row)
input("Press enter to continue.")
break
else:
employee_input == "2"
data = worksheet2.get_all_values()
for row in data:
print(row)
def new_employee_number():
"""
Generates a new employee number making sure it doesn't already exist, allows the user to enter their name and adds the data to the Employee Data sheet.
"""
new_employee_number = str(random.randint(0, 1000))
new_employee_name = input("Enter your name:\n")
while True:
if new_employee_name.isalpha() and new_employee_number not in worksheet3.col_values(1):
new_employee_name = new_employee_name.lower().capitalize()
worksheet3.append_row([new_employee_number, new_employee_name])
os.system('cls' if os.name == 'nt' else 'clear')
print(
f"Welcome, {new_employee_name}! Your employee number is: {new_employee_number}\n")
employee_action()
break
else:
print("INVALID: Enter only letters\n")
new_employee_name = input()
我尝试打破循环,但它似乎不起作用,我也问了chat-gpt,也没有对我有太大帮助。
答:
0赞
Brock Brown
5/5/2023
#1
如果你发布一个最小的可重现的例子,可能会更容易查明问题,但如果我不得不猜测,我会说有些东西在一遍又一遍地打电话。如果不显示调用的部分,就不可能分辨。new_employee_number()
new_employee_number()
0赞
Booboo
5/5/2023
#2
有些东西对我来说没有意义:在功能上,你有:employee_action
... # code omitted
else:
employee_input == "2"
data = worksheet2.get_all_values()
for row in data:
print(row)
表达式的计算结果为 True 或 False,但随后不使用该结果。如果您不想在用户输入“2”时重复循环,则您似乎也缺少语句。所以我对你的意思最好的猜测是:employee_input == 2
break
def employee_action():
"""
Asks the user what they wish to do now that they're logged in.
"""
while True:
employee_input = input(
"What do you wish to do today?\n1 Get Personal Data \n2 Get Survey Responses\n")
if employee_input == "1":
data = worksheet.get_all_values()
for row in data:
print(row)
input("Press enter to continue.")
break
elif employee_input == "2":
data = worksheet2.get_all_values()
for row in data:
print(row)
input("Press enter to continue.") # I am not sure if this is also missing
break
如果是这样,可以通过以下方法减少一些代码冗余:
def employee_action():
"""
Asks the user what they wish to do now that they're logged in.
"""
while True:
employee_input = input(
"What do you wish to do today?\n1 Get Personal Data \n2 Get Survey Responses\n")
if employee_input == "1":
data = worksheet.get_all_values()
elif employee_input == "2":
data = worksheet2.get_all_values()
else:
continue
for row in data:
print(row)
input("Press enter to continue.")
甚至:
def employee_action():
"""
Asks the user what they wish to do now that they're logged in.
"""
while employee_input := input(
"What do you wish to do today?\n1 Get Personal Data \n2 Get Survey Responses\n"
) not in ("1", "2"):
continue
if employee_input == "1":
data = worksheet.get_all_values()
else:
data = worksheet2.get_all_values()
for row in data:
print(row)
input("Press enter to continue.")
但是,如果您添加了第三个可能的选项,则这种交替重新编码将要求您在两个位置修改您的选择:(1)在循环中,您需要在可接受的选项列表中添加第三个选项,以及(2)您需要修改您的语句。因此,我更喜欢第一个备选方案。while
if ... else ...
评论