python_If_answers

#1. Write a Program to   Check if a number is positive or negative

num = int(input("Enter a number: "))
if num >= 0:
print("Even")
else:
print("Odd")

result = "Positive" if a >= 0 else "Negative"
print(result)

________________________________________________________________________________________________________________________

#2.Write a Program to Check if a person is eligible to vote (age 18 or above)

age = int(input("Enter age: "))
if age >= 18:
print("Eligible to vote")
else:
print("Not eligible to vote")

________________________________________________________________________________________________________________________
#3. Write a Program to Check if a number is even or odd

num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")

________________________________________________________________________________________________________________________
#4. Write a Program to Write a program to find the greatest of two numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

if a > b:
print("Greatest is : ", a)
else:
print("Greatest:", b)

result = "a is greater" if a > b else "b is greater"
print(result)

________________________________________________________________________________________________________________________

#5. Write a Program to Determine if a year is a century year

year = int(input("Enter a year: "))
if year % 100 == 0:
print("Century Year")
else:
print("Not a Century Year")
________________________________________________________________________________________________________________________

#5. Write a Program to Determine if a given year is a leap year or not.
Input 2000, 2004, 2008, 2020, 2022 years and test the logic.

year = int(input("Enter a year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap Year")
else:
print("Not a Leap Year")

________________________________________________________________________________________________________________________

#6.Write a Program to Determine if a number is a multiple of 5

num = int(input("Enter a number: "))
if num % 5 == 0:
print("Multiple of 5")
else:
print("Not a multiple of 5")

________________________________________________________________________________________________________________________

#7.Write a Program to Check if a character is a vowel or consonant

char = input("Enter a character: ").lower()

if (char =='a' or char =='e' char =='i' char =='o' char =='u') :
print("Vowel")
else:
print("Consonant")

if char in 'aeiou':
print("Vowel")
else:
print("Consonant")

________________________________________________________________________________________________________________________
#8.Write a Program to Determine if a person is eligible for a senior citizen discount (age 60+)

age = int(input("Enter age: "))
if age >= 60:
print("Eligible for senior citizen discount")
else:
print("Not eligible")

________________________________________________________________________________________________________________________
#9. Write a Program to Determine if a student passes or fails. if marks less than 50 then mark as fail.

marks = int(input("Enter marks: "))

if marks >= 40:
print("Pass")
else:
print("Fail")
________________________________________________________________________________________________________________________

#10. Write a Program to Check if a number is a single-digit number

num = int(input("Enter a number: "))
if 0 <= abs(num) < 10:
print("Single-digit number")
else:
print("Not a single-digit number")

________________________________________________________________________________________________________________________
#11.Write a Program to Determine if a number is between 1 and 100

num = int(input("Enter a number: "))

if 1 <= num <= 100:
print("Within range")
else:
print("Out of range")
________________________________________________________________________________________________________________________
#12. Write a Program to Check if a number is within a range (50-100)

num = int(input("Enter a number: "))
if 50 <= num <= 100:
print("Within range")
else:
print("Out of range")

________________________________________________________________________________________________________________________

#13. Write a Program to Check if a given time is AM or PM

hour = int(input("Enter hour (24-hour format): "))
if hour < 12:
print("AM")
else:
print("PM")

________________________________________________________________________________________________________________________
#14. Check if a string is empty or not

s = input("Enter a string: ")
if not s:
print("String is empty")
else:
print("String is not empty")

________________________________________________________________________________________________________________________

#15. Print "Weekend" if the day is Saturday or Sunday; otherwise, print "Weekday"

day = input("Enter a day: ").lower()
if day in ["saturday", "sunday"]:
print("Weekend")
else:
print("Weekday")



if day=='monday' or day=='tuesday' or day=='wednesday' or day=='thursday' or day=='friday':
print('weekday')
else:
print('HAPPY WEEKEND!')

if day=='saturday' or day=='sunday':
print('week-end')
else:
print('Weekday!')


________________________________________________________________________________________________________________________

#16. Find if a given number is exactly divisible by both 3 and 7

num = int(input("Enter a number: "))
if num % 3 == 0 and num % 7 == 0:
print("Divisible by 3 and 7")
else:
print("Not divisible by both")

________________________________________________________________________________________________________________________
#17. Check if the sum of two numbers is greater than 100

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if (a + b) > 100:
print("Sum is greater than 100")
else:
print("Sum is 100 or less")

________________________________________________________________________________________________________________________
#18. Write a program to find the minimum of two numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

if a < b:
print("Minimum:", a)
else:
print("Minimum:", b)

________________________________________________________________________________________________________________________
NESTED - IF

#19. Write a program to find the minimum of two numbers

number_str=input('enter a number')
number=int(number_str)

if number > 5:
print("Bigger than 5")
if number <= 15:
print("Between 5 and 15")

________________________________________________________________________________________________________________________

#20. Write a program to check the number is positive or negative. if positive the check if its even or odd.

number_str=input('enter a number')
number=int(number_str)
if number>0:
print('positive number')
if number%2==0:
print('even number')
if number%2!=0:
print('odd number')
else:
print('negative number')
number_str=input('enter a number')

________________________________________________________________________________________________________________________

#21. Write a program to find the minimum of two numbers

number=int(number_str)
if number%5==0:
print('multiple of 5')
if number%10==0:
print('multple of 10 as well!')
else:
print('not a multiple of 5')
character=input('enter the day')

________________________________________________________________________________________________________________________

#22. Write a program to read the age and member status and determine the ticket prices like below.
If person age is above 18 then charge the adult ticket price as 20.
If the person is above 18 and member then reduce the $2 of the price.
If person age is below 18 then charge the child ticket price as 10.
If the person is above 18 and member then reduce the $2 of the price.

age = 30
member = True

if age > 18:
if member:
print("Ticket price is $12.")
else:
print("Ticket price is $20.")
else:
if member:
print("Ticket price is $8.")
else:
print("Ticket price is $10.")

________________________________________________________________________________________________________________________


Multiple Conditions

#23. Check if a person is eligible for a driving car

age = int(input("Enter age: "))
passed_test = input("Did you pass the driving test? (yes/no): ").lower()

if age >= 18 and passed_test == "yes":
print("Eligible for a driving car")
else:
print("Not eligible")
________________________________________________________________________________________________________________________

#24. Check if a number is divisible by 2 but not by 3

num = int(input("Enter a number: "))
if num % 2 == 0 and num % 3 != 0:
print("Divisible by 2 but not by 3")
else:
print("Does not meet criteria")

________________________________________________________________________________________________________________________
ELIF

#25. Write a program to find the person age group

number_str=input('enter age: ')
age=int(number_str)

if age <= 12:
print("Child.")
elif age <= 19:
print("Teenager.")
elif age <= 35:
print("Young adult.")
else:
print("Adult.")

________________________________________________________________________________________________________________________

#26. Check if a triangle is valid given three side lengths.
the sum of any two sides is always greater than the length of the third side;
this is known as the Triangle Inequality Theorem.

a = int(input("Enter first side: "))
b = int(input("Enter second side: "))
c = int(input("Enter third side: "))

# method - 1
if (a + b <= c) or (a + c <= b) or (b + c <= a) :
print("Invalid Triangle")
else:
print("Valid Triangle")

# method - 2
if a + b > c and a + c > b and b + c > a:
print("Valid Triangle")
else:
print("Invalid Triangle")

________________________________________________________________________________________________________________________

#27. Find the largest of three numbers

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))

if a >= b and a >= c:
print("Largest:", a)
elif b >= a and b >= c:
print("Largest:", b)
else:
print("Largest:", c)

________________________________________________________________________________________________________________________
#28. Determine if a triangle is equilateral, isosceles, or scalene

a = int(input("Enter first side: "))
b = int(input("Enter second side: "))
c = int(input("Enter third side: "))

if a == b == c:
print("Equilateral Triangle")
elif a == b or b == c or a == c:
print("Isosceles Triangle")
else:
print("Scalene Triangle")

________________________________________________________________________________________________________________________

#29. Write a Program to Check if a number is positive, negative, or zero

num = int(input("Enter a number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")

res = "Positive" if num > 0 else "Negative" if num < 0 else "Zero"
print(res)

________________________________________________________________________________________________________________________

#30. Write a Program to Print "Good Morning" if the time is before 00:00 hrs to 12:00 PM, print "Good Afternoon".
If the time is between 12:01 to 18:00, otherwise print "Good Evening"

hour = int(input("Enter hour (24-hour format): "))

if 00 < hour < 12:
print("Good Morning")
elif 12 < hour < 18:
print("Good Afternoon")
else:
print("Good Evening")
________________________________________________________________________________________________________________________

#31. Check if a number is a palindrome
num = input("Enter a number: ")
if num == num[::-1]:
print("Palindrome")
else:
print("Not a Palindrome")

________________________________________________________________________________________________________________________

#32. Write a Program to Check if a bank account balance is sufficient for withdrawal

balance = float(input("Enter account balance: "))
withdraw = float(input("Enter withdrawal amount: "))

if balance >= withdraw:
print("Withdrawal successful")
else:
print("Insufficient funds")

________________________________________________________________________________________________________________________

IF - ELIF

#33. Determine the type of quadrilateral

a = int(input("Enter first side: "))
b = int(input("Enter second side: "))
c = int(input("Enter third side: "))
d = int(input("Enter fourth side: "))

if a == b == c == d:
print("Square")
elif a == c and b == d:
print("Rectangle")
else:
print("Other Quadrilateral")

________________________________________________________________________________________________________________________

#34. Calculate electricity bill

units = int(input("Enter electricity units consumed: "))

if units <= 100:
bill = units * 5
elif units <= 300:
bill = (100 * 5) + (units - 100) * 10
else:
bill = (100 * 5) + (200 * 10) + (units - 300) * 15
print("Total Bill: ", bill)

________________________________________________________________________________________________________________________

#35. Implement a basic calculator

a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
op = input("Enter operation (+, -, *, /): ")

if op == "+":
print("Result:", a + b)
elif op == "-":
print("Result:", a - b)
elif op == "*":
print("Result:", a * b)
elif op == "/":
print("Result:", a / b)
else:
print("Invalid operation")

________________________________________________________________________________________________________________________
#36. Find the grade of a student

marks = int(input("Enter marks: "))

if marks >= 90:
print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C")
elif marks >= 60:
print("Grade: D")
elif marks >= 40:
print("Grade: E")
else:
print("Grade: F (Fail)")


if 50<grade<60:
print('you got a c')
elif 60<grade<70:
print('you got a b')
elif 70<grade<80:
print('you got a A')
elif 80<grade<90:
print('you got a A star')
else:
print('better luck next time!')

________________________________________________________________________________________________________________________

#37. Implement a ticket pricing system

age = int(input("Enter age: "))

if age < 5:
print("Ticket Price: Free")
elif age >= 60:
print("Ticket Price: 50")
else:
print("Ticket Price: 100")

________________________________________________________________________________________________________________________

#38. Determine if a given alphabet is uppercase or lowercase

char = input("Enter an alphabet: ")
if char.isupper():
print("Uppercase")
else:
print("Lowercase")

________________________________________________________________________________________________________________________

#39. Implement a temperature converter

temp = float(input("Enter temperature: "))
unit = input("Enter unit (C/F): ").upper()
if unit == "C":
print("Fahrenheit:", (temp * 9/5) + 32)
elif unit == "F":
print("Celsius:", (temp - 32) * 5/9)
else:
print("Invalid unit")

________________________________________________________________________________________________________________________

#40. Check if a number is a power of 2

num = int(input("Enter a number: "))
if num > 0 and (num & (num - 1)) == 0:
print("Power of 2")
else:
print("Not a Power of 2")

________________________________________________________________________________________________________________________

#41. Determine how many days a month has

month = int(input("1 - jan , 2 - feb, 3 -mar and so on \n Enter month (1-12): "))
# 1 - jan , 2 - feb, 3 -mar and so on

year = int(input("Enter year: "))

if ( month==1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12 ):
month_days = 31
elif ( month == 4 or month == 6 or month == 9 or month == 11):
month_days = 30
elif month == 2:
month_days = 28
if ((year % 4 == 0) and (year % 100 == 0)) or (year % 400 == 0):
print("Leap year so feb month has 29 days")
month_days = 29

print(month_days)
#days = [31, 28 + (1 if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) else 0), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
#print("Days:", days[month - 1])

________________________________________________________________________________________________________________________

#42. Validate a password

import re

password = input("Enter password: ")
if len(password) >= 8 and re.search(r"[A-Za-z]", password) and re.search(r"\d", password):
print("Valid Password")
else:
print("Invalid Password")

________________________________________________________________________________________________________________________

#43. Verify if a number is a perfect square

import math
num = int(input("Enter a number: "))


if math.isqrt(num) ** 2 == num:
print("Perfect Square")
else:
print("Not a perfect square")

________________________________________________________________________________________________________________________

#44. Check if three numbers form a Pythagorean triplet

a, b, c = sorted(map(int, input("Enter three numbers: ").split()))

if a**2 + b**2 == c**2:
print("Pythagorean Triplet")
else:
print("Not a Pythagorean Triplet")

________________________________________________________________________________________________________________________

#45. Determine zodiac sign based on birthdate

month = int(input("Enter birth month (1-12): "))
day = int(input("Enter birth day: "))

zodiac = [("Capricorn", 20), ("Aquarius", 19), ("Pisces", 20), ("Aries", 20),("Taurus", 21), ("Gemini", 21), ("Cancer", 22), ("Leo", 22),("Virgo", 22), ("Libra", 23), ("Scorpio", 23), ("Sagittarius", 22), ("Capricorn", 31)]
sign = zodiac[month - 1][0] if day <= zodiac[month - 1][1] else zodiac[month][0]
print("Zodiac Sign:", sign)

________________________________________________________________________________________________________________________

#46. Validate an email format
import re

email = input("Enter email: ")

if re.match(r"^[\w\.-]+@[\w\.-]+\.(com|org|net|edu)$", email):
print("Valid Email")
else:
print("Invalid Email")

________________________________________________________________________________________________________________________

#47. Check if a knight move in chess is valid

x1, y1 = map(int, input("Enter current position (x y): ").split())
x2, y2 = map(int, input("Enter new position (x y): ").split())

if (abs(x1 - x2), abs(y1 - y2)) in [(2, 1), (1, 2)]:
print("Valid Knight Move")
else:
print("Invalid Move")

________________________________________________________________________________________________________________________

#48. Implement a loan eligibility checker

income = int(input("Enter monthly income: "))
credit_score = int(input("Enter credit score: "))
employed = input("Are you employed? (yes/no): ").lower()

if income >= 25000 and credit_score >= 700 and employed == "yes":
print("Loan Approved")
else:
print("Loan Denied")

________________________________________________________________________________________________________________________

#49. Implement a rock-paper-scissors game

import random
choices = ["rock", "paper", "scissors"]
user = input("Enter rock, paper, or scissors: ").lower()
computer = random.choice(choices)
print("Computer chose:", computer)
if user == computer:
print("It's a tie!")
elif (user == "rock" and computer == "scissors") or (user == "scissors" and computer == "paper") or (user == "paper" and computer == "rock"):
print("You win!")
else:
print("You lose!")
________________________________________________________________________________________________________________________

#50. Determine if a given date is valid

day = int(input("Enter day: "))
month_name = input("Enter month (mmm) : ")
month_name = month_name.lower()

year = int(input("Enter year (YYYY): "))
month_days=30

if (month_name=="january") or (month_name=="march") or (month_name=="may") or (month_name=="july") or (month_name=="august") or (month_name=="october") or (month_name=="december"):
month_days = 31
elif (month_name=="april") or (month_name=="june") or (month_name=="september") or (month_name=="november"):
month_days = 30
elif month_name=="february":
month_days = 28
if ((year % 4 == 0) and (year % 100 == 0)) or (year % 400 == 0):
print("Leap year so feb month has 29 days")
month_days = 29
else:
print("Invalid month")

if 1 <= day <= month_days:
print("Valid date")
else:
print("Invalid date")
________________________________________________________________________________________________________________________

Comments

Popular posts from this blog

Python_While_Loop

Python_Lists_Loops

clinical_app