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

Python双条件while循环问题:moneyihave计算异常与循环终止错误

Hey Jung, let's break down what's going wrong with your code and fix it step by step!

First, let's diagnose the two issues you mentioned:

1. Double deduction when inserting 200

In the money == 200 branch, you correctly subtract 200 from moneyihave with moneyihave = moneyihave - 200. But then your print statement uses moneyihave-200 again, which makes it look like you deducted twice. For example, if moneyihave was 40000 before, after subtracting 200 it becomes 39800—but your print shows 39800-200=39600, creating the false impression of a double deduction.

2. Loop only terminates when moneyihave hits -200

You put the loop termination checks (if not coffee and elif moneyihave<=0) inside the else branch. That means these checks only run when the user inserts too little money. When the user inserts enough money (200 or more), the code skips these checks and keeps looping, even if moneyihave drops to 0 or below. The loop only stops when you finally insert insufficient money, at which point moneyihave might already be negative.

Fixed Code with Explanations

# Initialize variables (added spaces for readability)
coffee = 20
coffee_price = 200  # Fixed variable name for clarity
money_i_have = 40000  # Renamed for readability

while True:
    # Check termination conditions FIRST, before asking for money
    if coffee <= 0:
        print('Please come back, we do not have coffee anymore')
        break
    if money_i_have < coffee_price:
        print("You don't have enough money left for another coffee")
        break
    
    money = int(input('Insert your money: '))
    
    if money == coffee_price:
        # Deduct cost and give coffee
        coffee -= 1
        money_i_have -= coffee_price
        print(f'Here is your coffee, we have {coffee} coffees left')
        print(f'You have {money_i_have} left in your pocket')  # Fixed: no extra deduction
    
    elif money > coffee_price:
        # Calculate change, deduct cost, give coffee and change
        change = money - coffee_price
        coffee -= 1
        money_i_have -= coffee_price
        print(f'Please take your change {change} and here is your coffee')
        print(f'You have {money_i_have} left in your pocket')  # Fixed: no extra deduction
    
    else:
        # Wrong amount inserted
        print(f'Our coffee costs {coffee_price} dollars')  # Fixed: corrected price from 300 to 200
        print(f'We have {coffee} coffees left')
        print(f'You have {money_i_have} left in your pocket')

Key Changes Made:

  • Renamed variables (e.g., coffeeprice to coffee_price, moneyihave to money_i_have) to make the code easier to read and maintain.
  • Moved termination checks to the start of the loop: Now we check if there's no coffee left, or if the user can't afford another coffee before asking for money. This prevents unnecessary input prompts when the loop should already end.
  • Fixed the double deduction display: Removed the extra -200 from all print statements showing remaining money—we already adjusted money_i_have correctly earlier in the code.
  • Corrected the price in the error message: Changed "300 dollars" to match the actual coffee_price of 200.
  • Used f-strings instead of format() for cleaner, more readable string formatting (this is a modern Python best practice).

This fixed code will terminate as soon as either coffee hits 0 or money_i_have is less than the coffee price (so the user can't buy another one), and it will display the correct remaining money every time.

内容的提问来源于stack exchange,提问作者Jung Lee

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 06:24:45