Page 509 - ComputerScience_Class_11
P. 509
Program 10.py
File Edit Format Run Options Window Help
items = {'Coke': 40, 'Chips': 20, 'Candy': 10}
print("Items available: Coke, Chips, Candy")
while True:
choice = input("Enter the item you want to buy (or 'quit' to exit): ").strip()
if choice == 'quit':
break
if choice not in items:
print("Invalid choice, please try again.")
continue # Skip to next iteration if choice is invalid
money = float(input("The price of " + choice + " is Rs." + str(items[choice])
+ ". Enter your money: "))
if money < items[choice]:
print("Insufficient funds, please insert more money.")
continue # Skip to next iteration if money is not enough
change = money - items[choice]
print("Enjoy your " + choice + "! Your change is Rs." + str(change) + ".")
Output
Items available: Coke, Chips, Candy
Enter the item you want to buy (or 'quit' to exit): Coke
The price of Coke is Rs.40. Enter your money: 60
Enjoy your Coke! Your change is Rs.20.0.
Enter the item you want to buy (or 'quit' to exit): quit
Let’s Revisit
♦ Flow of control refers to the sequence in which statements or function calls are executed within a program.
♦ In Python, indentation refers to the spaces or tabs used at the beginning of a line of code to define the structure of the program.
♦ In Python, sequential flow of control refers to the default order in which instructions in a program are executed.
♦ Conditional flow of control allows a program to make decisions based on certain conditions.
♦ The if statement is one of the most fundamental control structures in Python, allowing the program to make decisions based
on a condition.
♦ The if-else statement is one of the most commonly used control structures in programming.
♦ The nested if statement in Python is a way of placing one if statement inside another.
♦ Iterative statements, also known as loops, allow a program to execute a block of code repeatedly, based on a condition.
♦ The for loop is a type of iterative statement in Python that allows you to repeat a block of code a specific number of times or
iterate over a sequence (like a list, tuple, string or range).
♦ The pass statement is a placeholder that does nothing.
♦ The while loop in Python is a type of iterative statement that is used to repeat a block of code as long as a specified condition
is true.
Flow of Control 507

