提问人:Art 提问时间:11/1/2023 最后编辑:mkrieger1Art 更新时间:11/1/2023 访问量:43
我该怎么做,当我输入 chairsale < 0 时,如果我只想让它循环到椅子输入直到它不低于 0,它不会循环回桌面输入
How do I make it so when I input chairsale < 0 it doesn't loop back to desktable input if I just want it to loop on chair input till it isn't below 0
问:
saleperson = int(input("Please input salesperson ID or -1 to end: "))
while saleperson != -1:
desksale = int(input("Enter desk sales: "))
if desksale < 0:
print("Invalid desk sales please enter a positive number")
else:
tablesale = int(input("Enter table sales: "))
if tablesale < 0:
print("Invalid table sales please enter a positive number")
else:
chairsale = int(input("Enter chair sales: "))
if chairsale < 0:
print("Invalid chair sale please enter a positive number")
else:
saleperson = int(input("Please input salesperson ID or -1 to end: "))
每当我输入 -1 表示 tablesale 时,它都会返回要求 desksale,我希望它只是重复询问 tablesale,直到我输入一个正数。
我的代码应该基于这个评分标准
创建一个程序,提示用户输入销售人员的 o 身份证 o 餐桌销售 o 办公桌销售 o 椅子销售 如果用户输入了无效数据(例如负椅销售),则消息应 出现“无效:输入有效的椅子销售:” o 在用户输入有效数据之前,程序不应继续 然后,程序根据上表计算销售佣金 然后,程序应提示用户是否处理另一个销售人员 o 应重复该过程,直到用户决定不继续 一旦用户决定不输入其他销售人员数据,两个表应 出现销售表和佣金表 Sales 表格显示在每一行上 o 每个销售人员的身份证、桌子销售、办公桌销售和椅子销售 o 最后一行显示桌子总销售额、办公桌总销售额和椅子总销售额 佣金表格显示在每一行上 o 每个销售人员的身份证、餐桌佣金、办公桌佣金和椅子 委员会 o 最后一行显示总表佣金、总台佣金和总计 委员会主席
答:
您可以通过使用嵌套循环反复请求椅子销售来实现此目的,直到输入正数。下面介绍了如何修改代码以实现此目的:
# Initialize variables for total sales and total commissions
total_table_sales = 0
total_desk_sales = 0
total_chair_sales = 0
total_table_commission = 0
total_desk_commission = 0
total_chair_commission = 0
# Outer loop for entering salesperson data
while True:
saleperson = int(input("Please input salesperson ID or -1 to end: "))
if saleperson == -1:
break
desksale = int(input("Enter desk sales: "))
if desksale < 0:
print("Invalid desk sales please enter a positive number")
continue
tablesale = int(input("Enter table sales: "))
if tablesale < 0:
print("Invalid table sales please enter a positive number")
continue
# Inner loop for chair sales
while True:
chairsale = int(input("Enter chair sales: "))
if chairsale < 0:
print("Invalid chair sales please enter a positive number")
else:
break
# Calculate commissions for each type of sale
table_commission = tablesale * 0.1
desk_commission = desksale * 0.15
chair_commission = chairsale * 0.05
# Update total sales and total commissions
total_table_sales += tablesale
total_desk_sales += desksale
total_chair_sales += chairsale
total_table_commission += table_commission
total_desk_commission += desk_commission
total_chair_commission += chair_commission
# Display the sales and commission tables
print("Sales Table:")
print("Salesperson ID | Table Sales | Desk Sales | Chair Sales")
print(f"{saleperson} | {tablesale} | {desksale} | {chairsale}")
print("Total | {} | {} | {}".format(total_table_sales, total_desk_sales, total_chair_sales))
print("Commission Table:")
print("Salesperson ID | Table Commission | Desk Commission | Chair Commission")
print(f"{saleperson} | {table_commission} | {desk_commission} | {chair_commission}")
print("Total | {} | {} | {}".format(total_table_commission, total_desk_commission, total_chair_commission))
此代码将继续要求椅子销售,直到输入正数,并且它将正确计算总销售额和佣金。
您需要有多个 while 循环来检查条件,因为您需要检查每个输入是否都是有效数字并检查它们,直到用户输入有效输入。我已经根据您发布的评分标准调整了代码。
saleperson = int(input("Please input salesperson ID or -1 to end: "))
while saleperson != -1:
desksale = int(input("Enter desk sales: "))
while desksale < 0:
desksale = int(input("Invalid desk sales please enter a positive number:"))
tablesale = int(input("Enter table sales: "))
while tablesale < 0:
tablesale = int(input("Invalid table sales please enter a positive number:"))
chairsale = int(input("Enter chair sales: "))
while chairsale < 0:
chairsale = int(input("Invalid chair sale please enter a positive number:"))
saleperson = int(input("Please input salesperson ID or -1 to end: "))
在这里,我确保所有待售输入都不是负数,当用户进入程序时,程序将立即停止。-1
saleperson
仅当用户仅输入整数时,这才有效。应尝试验证输入是否为数字,并且在用户输入字符串时不会遇到错误。
当您键入 -1 时,您的代码返回请求 desksale 的原因是该语句的计算结果为 True,导致您的程序执行,然后返回到 while 循环的开头,因为 saleperson 仍然< 0。if tablesale < 0
print("Invalid table sales please enter a positive number")
不要使用 saleperson 变量作为 while 循环的条件,而是尝试使用 Boolean 标志,您可以根据其他变量赋值切换到 True 或 False,或者只使用 .尽管这是一种不鼓励的方法,但只要您小心放置语句以防止无限循环,它就可以完成工作。while True
break
在获得预期的答案之前,我也不建议您将用户锁定在问题中。这是一种更好的方法,向用户抛出一个解释性错误,然后从头开始所有提示。像这样的东西:
while True:
saleperson = int(input("Please input salesperson ID or -1 to end: "))
if saleperson == -1:
break # Here’s your exit
desksale = int(input("Enter desk sales: "))
if desksale < 0:
print("Invalid desk sales! Please enter a positive number or 0")
continue # Restart the loop
tablesale = int(input("Enter table sales: "))
if tablesale < 0:
print("Invalid table sales! Please enter a positive number or 0")
continue # Restart the loop
chairsale = int(input("Enter chair sales: "))
if chairsale < 0:
print("Invalid chair sales! Please enter a positive number or 0”)
# No need to restart the loop as it will automatically restart because there is no more code to execute
评论