Putting it Together I
Let's put a bunch of concepts together.
Let's create a basic checkbook to keep track of how much money we have.
together.py
balance = 0 option = '' while option != 'q': option = input(f'${balance} A)dd Money S)ubtract Money Q)uit:') match option: case 'a': print('Add money') # Ask user for how much money to add # Update the balance variable case 's': print('Subtract money') # Ask user for how much money to subtract # Update the balance variable
Output:
$0 A)dd Money S)ubtract Money Q)uit:a Add money $0 A)dd Money S)ubtract Money Q)uit:s Subtract money $0 A)dd Money S)ubtract Money Q)uit:q Done.
Links
Exercises (5 mins)
-
Complete the together.py program. And a case statement for when the user enters an invalid value.
$0 A)dd Money S)ubtract Money Q)uit:j Invalid option $0 A)dd Money S)ubtract Money Q)uit:a Add money Enter amount:10 $10 A)dd Money S)ubtract Money Q)uit:s Subtract money Enter amount:20 $-10 A)dd Money S)ubtract Money Q)uit:a Add money Enter amount:30 $20 A)dd Money S)ubtract Money Q)uit:q Done.