Conditionals Exercises#
Warm Up Question#
Convert the variable \(x = 2\) to a string, assign it to the variable \(y\), and then display the type of \(y\).
Warm Up Question#
Determine the output of the code below
print(5 > 3)
print(10 > 20)
Warm Up Question#
Determine the output of the code below
x = 5
if x > 3:
print('A')
Warm Up Question#
Determine the output of the code below
x = 5
if x < 3:
print('A')
Warm Up Question#
Determine the output of the code below
if 1 > 4:
print('A')
else:
print('B')
Warm Up Question#
Write a program that displays hot if the variable temperature is greater than 80; otherwise, it should display not hot.
Test your code using temperature values of 100 and 50.
Warm Up Question#
Determine the output of the code below
if 6 > 4:
print('A')
if 7 < 5:
print('B')
else:
print('C')
Warm Up Question#
Write a program that prompts the user to enter a name and returns the 5th character.
Use a try-except statement to warn the user if the name contains fewer than 5 characters.
Question-1#
Prompt the user to enter a five-digit number and calculate the count of even digits.
Do not use loops.
Sample output:
Please enter a five digit number: 12345
There are 2 even digits in 12345.
Question-2#
The following code returns the week day of today as a number. (0 is Monday, 1 is Tuesday, ….)
from datetime import datetime
datetime.now().weekday()
Prompt the user to enter a weekday (Monday, Tuesday,…).
Verify whether today is the weekday given by the user. (not case-sensitive)
Do not use loops or lists.
Sample output:
Please enter a weekday: ThursdaY
Today is not Thursday.
Please enter a weekday: wedNESday
Today is Wednesday.
Please enter a weekday: wed
Warning!!! Please enter the day in the correct form.
Question-3#
Use a single input() function to prompt the user to enter the coordinates of a theater in the format (x,y).
Assume the user’s house coordinates are (3,4).
Calculate the distance between the house and the theater using the distance formula.
If the distance is less than 5 units, the user will go to the theater; otherwise, the user will stay at home.
Display the conclusion based on the provided theater location.
The distance between two points \(P=(x_1,y_1)\) and \(Q=(x_2,y_2)\) is given by the following distance formula:
\(\displaystyle dist(P,Q) = \sqrt{(x_2-x_1)^2+(y_2-y_1)^2}\)
Sample Output:
Enter the coordinates of the theater in the form of (x,y): (2,10)
The theater is too far. Stay at home.
Question-4#
Prompt the user to enter a word.
If the given word has an odd length, print the middle character. Otherwise, print the middle two characters
Sample output:
Enter a word: abcdef
Middle characters: cd
Enter a word: abcdefg
Middle character: d
Question-5#
Prompt the user to choose a password that satisfies the following conditions:
The first character must be a digit.
The last character must be a punctuation mark.
The length of the password must be at least 5 characters.
Print whether the given password is acceptable.
Sample output:
Choose a password: 123.
The password 123. is NOT acceptable.
Choose a password: abcdef,
The password abcdef, is NOT acceptable.
Choose a password: 2werqt,
The password 2werqt, is acceptable.
Question-6#
Prompt the user to choose a number between 1 and 6, including these numbers.
Roll a die (choose a random number between 1 and 6).
Check whether the user’s guess is correct.
Sample Output:
Guess the number on the die: 9
Please enter a valid number.
Guess the number on the die: 5
Wrong!
Guess the number on the die: 2
Correct!
Question-7: Absolute Value#
Ask the user for a number and find its absolute value without using built-in functions.
Absolute value means the positive value of a number.
For example, if you have -5, the absolute value is 5.
If the number is 7, its absolute value remains 7.
Absolute value of 0 is simply 0.
Sample Output:
Please enter a number: -5
Absolute value of -5.0 is 5.0
Please enter a number: 7
Absolute value of 7.0 is 7.0
Question-8: ReLU#
Ask the user for a number and find its ReLU value without using built-in functions.
ReLU value of positive numbers is itself.
ReLU of 0 is 0.
ReLU value of negative numbers is 0.
Examples:
ReLU value of 7 is 7.
ReLU value of 0 is 0.
ReLU value of -5 is 0.
Sample Output:
Please enter a number: -5
ReLU value of -5.0 is 0
Please enter a number: 7
Relu value of 7.0 is 7.0
Question-9: Heaviside#
Ask the user for a number and find its Heaviside value without using built-in functions.
Heaviside value of a positive number is 1.
Heaviside value of a negative number is 0.
Heaviside value of 0 is 0.5
Sample Output:
Please enter a number: -5
Heaviside value of -5.0 is 0
Please enter a number: 7
Heaviside value of 7.0 is 1
Please enter a number: 0
Heaviside value of 0.0 is 0.5
Business Applications#
Question: Discount#
An online store offers discounts based on the amount you spend:
No discount for spending less than 100 dollars.
20% discount for spending between 100 and 150 dollars.
30% discount for spending between 150 and 200 dollars.
40% discount for spending over 200 dollars.
Write a program that prompts the user to enter their total spending and returns the final price after applying the appropriate discount, adding a 6% tax, and including a 3% shipping fee.
Question: Loan Approval#
Write a program that prompts the user to enter their credit score and the amount they want to apply for using two input() functions. The program should return the approval decision based on the following table:
Credit Score |
Loan Amount |
Decision |
---|---|---|
less than or equal to 600 |
less than or equal to 10,000 |
Approved |
less than or equal to 600 |
greater than 10,000 |
Deny |
between 600 and 800 |
less than or equal to 30,000 |
Approved |
between 600 and 800 |
greater than 30,000 |
Deny |
greater than or equal to 800 |
less than or equal to 50,000 |
Approved |
greater than or equal to 800 |
greater than 50,000 |
Deny |
Question: Tax#
IRS uses he following table to determine the tax amount for a single person.
Tax Rate |
on taxable income from … |
up to … |
---|---|---|
10% |
0 |
11,000 |
12% |
11,001 |
44,725 |
22% |
44,726 |
95,375 |
24% |
95,376 |
182,100 |
32% |
182,101 |
231,250 |
35% |
231,251 |
578,125 |
37% |
578,126 |
and up |
You pay tax based on income levels called tax brackets.
As your income increases, the tax rate for each new level goes up.
When you move to a higher tax bracket, you don’t pay the higher rate on all your income.
You only pay the higher rate on the income within the new bracket.
Example If your income is 100,000 dollars, the tax you pay is the sum of the taxes from each tax bracket your income falls into. Therefore it is the sum of:
10% of 11,000
12% 0f 44,725 - 11,001 = 33,724
22% of 95,375 - 44,726 = 50,649
24% 0f 100,000 - 95,375 = 4,625 Source: https://www.irs.gov/filing/federal-income-tax-rates-and-brackets
Write a program that prompts the user to enter their income and returns the corresponding tax amount based on th etable above.
Question: Bucket Strategy#
A finance company offers three investment products based on different risk levels:
Conservative: 60% in interest, 30% in gold, 10% in stocks.
Moderate: 30% in interest, 40% in gold, 30% in stocks.
Aggressive: 10% in interest, 30% in gold, 60% in stocks.
Write a program that prompts the user to enter their investment amount and the type of investment using two input() functions.
The program should then calculate and return the gain or loss based on the following rates: an interest gain rate of 3%, a gold gain rate of 2%, and a stock loss rate of 3.5%.
The risk level input is not case-sensitive.
Sample Output:
Please enter the risk type (Conservative/Moderate/Aggressive): Moderate
Please enter the investment amount: 1800
Change: 11.7