Task 3 Programming Language Kelompok 13
Task 3 Programming Language Kelompok 13
DOSEN PENGAMPU :
NAMA ANGGOTA :
Code
Detailed Explanation of the Code
The Python code provided helps Beni, a fruit seller, to make decisions on which fruit to sell to
customers based on their budget. The code uses conditional logic to evaluate the price of the
fruit about the customer's budget and provides suggestions accordingly. Here’s a step-by-step
breakdown of each part of the code.
1. Function Definition:
This line defines a function named buy_fruit that takes two parameters:
• fruit_price: The price of the fruit that Beni is considering selling.
• budget: The amount of money the customer has available to spend.
A function is a reusable block of code that can perform a task. Here, the task is to evaluate the
relationship between the fruit’s price and the customer's budget and print out appropriate
suggestions.
2. Conditional Statements:
The core of the function is a series of conditional if-elif-else statements that determine what
message to print based on the price of the fruit and the customer's budget.
a. If the price is higher than the customer's budget:
• The first condition checks if the fruit_price is greater than the budget.
• If this condition is true, it means the fruit is too expensive for the customer. The program
prints a message: "The price is too expensive, choose a cheaper fruit."
b. If the price matches the customer's budget exactly:
• This condition checks if the fruit_price is less than half of the budget.
• If true, the program prints: "The price is very cheap, you can buy more fruits." This
suggests that since the fruit is cheap compared to the budget, the customer might be
interested in buying more than one fruit.
d. If the price is reasonable but not very cheap or expensive:
• If none of the previous conditions are met, the program runs this else block. This means
the fruit_price is less than the budget but not so cheap that the customer can buy more.
• The message printed is: "The price is reasonable, you can buy this fruit."
3. Input from User:
• The program prompts the user to input two values: their budget and the price of the fruit
they are considering.
o budget: The customer's total money, is entered through the input() function,
which takes user input as a string. We convert it to a float to allow decimal
numbers, as prices and budgets often include decimal points (e.g., Rp 10,500).
o fruit_price: The price of the fruit, also entered by the user and converted to a
float.
4. Function Call:
• This line calls the buy_fruit function, passing the fruit_price and budget values obtained
from the user input.
• The function evaluates these inputs using the if-elif-else conditions and prints an
appropriate message based on our described logic.
• If none of the previous conditions (in the if and elif statements) are true, the else
block is executed.
• In this case, it prints: "The price is reasonable, you can buy this fruit."
3. if-elif Statement
The if-elif structure is used when you have multiple conditions to check. After the if
statement, elif (which stands for "else if") allows you to check additional conditions if the
first condition is false.
In the code:
• elif fruit_price == budget checks if the fruit_price is equal to the budget. If this
condition is true, it prints: "The price is exact, you can buy this fruit."
• If that condition is false, the next elif is checked: fruit_price < budget / 2. If this
condition is true, the program prints: "The price is very cheap, you can buy more
fruits."
Output
Example 1:
• Customer’s budget: Rp 25,000
• Fruit price: Rp 10,000
Example 2:
• Customer’s budget: Rp 12,000
• Fruit price: Rp 15,000
Example 3:
• Customer’s budget: Rp 15,000
• Fruit price: Rp 15,000
Summary:
• if is used for the first condition.
• if-else allows handling two possible outcomes: one if the condition is true, another if
it’s false.
• if-elif allows checking multiple conditions after the initial if if the previous conditions
are not met.
This code helps Beni quickly determine the best course of action when selling fruits to
customers with varying budgets. The program evaluates whether the price is too high, matches
the budget, or is cheap enough for the customer to buy more, and it prints a suggestion
accordingly. This kind of decision-making process is essential in maximizing sales and
ensuring customer satisfaction in real-life situations at the fruit stand.