Conditionals Exercises#

Section Title: Conditionals Exercises

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