You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python嵌套循环实现动态金额支付验证的技术求助

解决方案

直接修改你现有代码的支付部分,添加嵌套while循环来验证支付金额是否足够,以下是调整后的完整代码:

import random  # 需导入random模块,否则random.uniform会报错

# 假设total和capitalized_string是你之前已计算好的变量(示例值,实际由你的餐费逻辑生成)
total = 50.0
capitalized_string = "Smith"

while True:
    coup = input("Do you have a lucky draw coupon? 1 for YES 2 for NO: ")
    if coup == "1":
        # 生成1-5美元的随机优惠券,保留两位小数
        coupon = round(random.uniform(1.00, 5.00), 2)
        print()
        print("---------------------")
        print(f"You will save ${coupon:.2f}!")
        print("---------------------")
        print()
        newtotal = total - coupon
        print(f"Your new total is: ${newtotal:.2f}")
        print()
        
        # 嵌套循环:重复验证支付金额是否足够
        while True:
            payment = float(input("Please enter your payment amount: "))
            if payment >= newtotal:
                # 金额足够,计算找零并退出嵌套循环
                change2 = payment - newtotal
                change2 = round(change2, 2)
                print(f"Change: ${change2:.2f}")
                break
            else:
                # 金额不足,提示重新输入
                print(f"Insufficient amount! Please enter at least ${newtotal:.2f}")
        
        print()
        print("---------------------------------------------------------------------")
        print("Thank you party of", capitalized_string, "for choosing Erin's Cafe! Have a great day!")
        print("---------------------------------------------------------------------")
        print()
        break
    elif coup == "2":
        print()
        print("That's ok! Maybe next time!")
        print()
        
        # 嵌套循环:重复验证支付金额是否足够
        while True:
            payment = float(input("Please enter your payment amount: "))
            if payment >= total:
                # 金额足够,计算找零并退出嵌套循环
                change = payment - total
                change = round(change, 2)
                print(f"Change: ${change:.2f}")
                break
            else:
                # 金额不足,提示重新输入
                print(f"Insufficient amount! Please enter at least ${total:.2f}")
        
        print()
        print("---------------------------------------------------------------------")
        print("Thank you party of", capitalized_string, "for choosing Erin's Cafe! Have a great day!")
        print("---------------------------------------------------------------------")
        print()
        break
    else:
        print()
        print("Invalid response. Please type 1 for YES and 2 for NO: ")

关键改动说明

  • 嵌套while循环:在两个支付输入环节分别添加while True循环,实现重复提示输入的逻辑。
  • 金额判断逻辑:每次输入支付金额后,判断payment >= 应付总金额(有优惠券时为newtotal,无优惠券时为total)。满足条件则计算找零并退出嵌套循环;不满足则提示用户重新输入。
  • 逻辑简化:把原有的payment - total + coupon简化为payment - newtotal,因为newtotal = total - coupon,让找零计算逻辑更直观。

内容的提问来源于stack exchange,提问作者Erin McKee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 00:25:18