Conditionals Code#

Section Title: Conditionals Code

  • Please solve the following questions using Python code.  

Question-1#

Write a program that asks the user to enter a 6-digit number.

  • Check whether the digit in the thousands place is even.

Solution

number = input('Please enter a 6 digit number: ')

thousands_digit = int(number[2])

if thousands_digit % 2:
    print(f'Thousands digit is odd.')
else:
    print(f'Thousands digit is even.')

Question-2#

Write a program that asks the user to enter a percent grade.

  • Display the corresponding “Letter Grade” according to the following grading scale.

Letter Grade

Grade Range

A

96 - 100

A-

90 - 95

B+

87 - 89

B

84 - 86

B-

80 - 83

C+

77 - 79

C

74 - 76

C-

70 - 73

D+

67 - 69

D

64 - 66

D-

60 - 63

F

0 - 59

Solution

grade = float(input('Please enter your percent grade: '))

if grade >= 96: print('A')
elif grade >= 90: print('A-')
elif grade >= 87: print('B+')
elif grade >= 84: print('B')
elif grade >= 80: print('B-')
elif grade >= 77: print('C+')
elif grade >= 74: print('C')
elif grade >= 70: print('C-')
elif grade >= 67: print('D+')
elif grade >= 64: print('D')
elif grade >= 60: print('D-')
else: print('F')

Question-3#

The piecewise-defined function \(f(n)\) is given as follows:

\[\begin{split} f(n) = \begin{cases} n^2+3n+4 & n > 7 \\ 3n-7 & 7 \ge n >4 \\ 10 & 4 \ge n \ge 2\\ 3-4n & \text{otherwise}\\ \end{cases} \end{split}\]
  • Write a program that asks the user to enter an integer.

  • If the integer entered is n then display f(n).

  • Hint:

    • If n is greater than 7 then f(n)= \(n^2+3n+4\).

    • If n is between 4 (4 is not included) and 7 (7 is included) then f(n)=3n-7.

    • If n is between 2 (2 is not included) and 4 (4 is included) then f(n)=10.

    • If n is less than 2 then f(n)=3-4n.

Solution

n = int( input('Enter an integer: ')  )

if n > 7:
    print(f'f({n}) = {n**2+3*n+4}')
elif 4 < n:
    print(f'f({n}) = {3*n-7}')
elif 2 <= n:
    print(f'f({n}) = 10')
else:
    print(f'f({n}) = {3-4*n}')

Question-4#

Temperature Converter: Fahrenheit (F) <—> Celsius (C).

  • Ask for temperature with unit (F or C) from the user.

    • Use only one input function.

    • The user should enter the temperature in the form (int)F or (int)C.

      • Example: 37F or 42C

  • If the temperature is given in Fahrenheit (F) by the user, then convert it to Celsius (C).

  • If the temperature is given in Celsius (C) by the user, then convert it to Fahrenheit (F).

  • Display the converted temperature with its unit.

Solution

temperature_unit = input('Please enter the temperature with the unit: ')

temp = int(temperature_unit[:-1])     # the slice, except for the last character, represents the numerical value of the temperature.
unit = temperature_unit[-1]           # the last character is the unit

if unit == 'F':
    print(f'It is {(temp-32)/1.8}C.')
else:
    print(f'It is {temp*1.8+32}F.')

Question-5#

Ask the user to input a non-negative integer.

  • Check if this integer is a perfect square and print your conclusion.

    • Perfect squares are squares of integers, for example: 0, 1, 4, 9, 16, 25, 36, 49, etc.

  • If the given number is a negative integer, print a warning message.

  • Utilize the sqrt function from numpy or math.

  • Example:

    • For number = -3, output: -3 < 0. Please enter a non-negative integer.

    • For number = 4, output: 4 is a perfect square.

    • For number = 12, output: 12 is NOT a perfect square.

Solution

import math
number = int(input('Enter a non-negative integer: '))

if number < 0:
  print(f'{number} < 0. Please enter a non-negative integer.')
else:
  int_sqrt = int(math.sqrt(number))           # if a number is a perfect square, its square root is an integer
  if int_sqrt**2 == number:                   # so the square root's integer part is itself
    print(f'{number} is a perfect square.')
  else:
    print(f'{number} is a NOT perfect square.')

Question-6#

Ask for a name from the user.

  • Check if it contains the letters ‘k’ or ‘K’ and if the length of the name is an odd number.

  • If this is the case, then replace ‘k’ with ‘y’ and ‘K’ with ‘Y’.

  • Display the new name.

  • Example:

    • name = Tom Output: Tom

    • name = Jack Output: Jack (length is even)

    • name = KaThy Output: YaThy

    • name = katHy Output: yatHy

Solution

name = input('Enter a name: ')

if ('k' in name.lower()) & (len(name)%2==1):
  print(name.replace('k','y').replace('K','Y'))
else:
  print(name)

Question-7#

Ask for a number from the user

  • Check whether the given number satisfies the following inequality: \(x^2-3x+6>192\)

  • Display the conclusion.

Solution-1

x = float(input('Enter a number: '))

y = x**2-3*x+6

if y > 192:
  print(f'{x} satisfies the inequality')
else:
  print(f'{x} does NOT satisfy the inequality')

Solution-2

x = float(input('Enter a number: '))

inequality = (x**2-3*x+6) > 192

if inequality:
  print(f'{x} satisfies the inequality')
else:
  print(f'{x} does NOT satisfy the inequality')

Question-8#

Ask for Test-1, Test-2, and Final exam grades from the user.

  • Use three input functions.

  • Compute the weighted average by using the following formula:

    • Weighted Average = 0.2 x Test-1 + 0.3 x Test-2 + 0.5 x Test-3

  • Find the letter grade by using the following grading scale.

Weighted Average

Letter Grade

75-100

A

60-74

B

40-59

C

0-39

F

  • Example:

    • Test-1 grade: 70

    • Test-2 grade: 80

    • Final grade: 90

    • Weighted average = \(0.2 \times 70 + 0.3 \times 80 + 0.5 \times 90 = 14 + 24 + 45 = 83\)

    • Output:

      • Weighted Average 83.0 —-> Letter Grade: A

Solution


test1 = float(input('Test-1 grade: '))
test2 = float(input('Test-2 grade: '))
final = float(input('Final  grade: '))

weighted_grade = test1*0.2+test2*0.3+final*0.5

print(f'Weighted Average {weighted_grade}   ---->   Letter Grade: ', end='')

if   100 >= weighted_grade >= 75: 
    print('A')
elif weighted_grade >= 60: 
    print('B')
elif weighted_grade >= 40:
    print('C')
else:
    print('F')

Question-9#

  • Print the statement: Basic Calculator

  • Print 20 dashes (-)

  • Ask the user for two numbers using two input functions

  • Ask the user for the operation to perform

    • Provide the options: +, -, x, /

    • The user should choose one of them.

  • Find the result

    • Perform the operation: First Number operation Second Number.

    • If the second number is zero, do not perform the division operation and display a warning message.

    • If the operation is division, also round the result.

  • Example: num1=5, num2=7, operation=’x’ —> Output: 5x7=35

Solution

print('Basic Calculator')
print('-'*20)

number1 = float(input('Number-1: '))
number2 = float(input('Number-2: '))
operation = input('Operation: (+,-,x,/): ')

if   operation == '+': 
    print(f'{number1} {operation} {number2} = {number1 + number2}')
elif operation == '-': 
    print(f'{number1} {operation} {number2} = {number1 - number2}')
elif operation == 'x': 
    print(f'{number1} {operation} {number2} = {number1 * number2}')
else:
  if number2 == 0: 
      print('Warning: Division by zero.')
  else: 
      print(f'{number1} {operation} {number2} = {number1 / number2:.2f}')

Question-10#

Ask the user for a number with at most 8 digits.

  • If the given number has fewer than 8 digits, pad zeros to the left to make it an 8-digit number.

  • Example:

    • Given number: 123 —> Output: 00000123

    • Given number: 123456789 —> Output: Please enter a number with at most 8 digits.

    • Given number: 12345678 —> Output: 12345678

Solution

number = input('Please enter a number with at most 8 digits: ')

length = len(number)

if length <= 8:
  print((8-length)*'0'+number)
else:
  print('Please enter a number with at most 8 digits.')