如何在PyCharm中为多变量添加多个if条件(板球装备计算程序)
Hey there! Since you're just starting out with PyCharm and programming, let's break this down into simple, easy-to-follow steps. I'll walk you through writing a program that meets your requirements, with explanations so you understand each part.
First, let's formalize the requirements we have:
- Calculate total equipment cost for 3 new cricket players
- Each player gets knee pads, batting gloves, and a T-shirt (we need to ask for their T-shirt size)
- Sum up all costs to get the final total
Let's assign realistic base prices to each item (you can adjust these numbers to match actual costs):
- Knee pads: $50 per pair
- Batting gloves: $40 per pair
- T-shirt: $30 each (we'll assume size doesn't affect cost for now, but we can adjust this later if needed)
Here's a complete, commented Python program you can use directly in PyCharm:
# Define fixed costs for each piece of equipment (edit these values as needed) KNEE_PADS_COST = 50.0 BATTING_GLOVES_COST = 40.0 T_SHIRT_COST = 30.0 # Initialize total cost to 0 before calculating total_equipment_cost = 0.0 # Loop through each of the 3 players for player_number in range(1, 4): print(f"\n--- Player {player_number} ---") # Ask for the player's T-shirt size and store the response shirt_size = input("Please enter your T-shirt size (e.g., S, M, L, XL): ") # Calculate cost for this single player individual_player_cost = KNEE_PADS_COST + BATTING_GLOVES_COST + T_SHIRT_COST # Add this player's cost to the overall total total_equipment_cost += individual_player_cost # Optional: Print a breakdown for the player print(f"Equipment cost for Player {player_number}: ${individual_player_cost:.2f}") # Display the final total cost print("\n" + "="*40) print(f"FINAL TOTAL EQUIPMENT COST: ${total_equipment_cost:.2f}")
How this program works:
- Constants: We use uppercase variables for fixed costs (like
KNEE_PADS_COST) so they're easy to locate and update later if prices change. - Loop: The
forloop runs exactly 3 times (for players 1, 2, 3) to process each athlete individually. - User Input: The
input()function prompts the user to enter a T-shirt size, which we store even though it doesn't affect cost right now (you can add size-specific pricing later if needed). - Cost Calculation: For each player, we sum their three equipment items and add that to the running total.
- Clean Output: We format all currency values to 2 decimal places for readability, and use separators like
---and===to make the output easy to follow.
Running this in PyCharm:
- Create a new Python file (File > New > Python File) and name it something like
cricket_equipment_cost.py. - Paste the code above into the file.
- Click the green "Run" button in the top-right corner, or right-click the file and select "Run".
- Enter each player's size when prompted in the terminal at the bottom of the screen.
If you need to modify the code (like adding different costs for different T-shirt sizes or changing equipment items), just tweak the constants or add conditional logic—let me know if you need help with that!
内容的提问来源于stack exchange,提问作者Anand Vaswani

