ques: 1] Read a number & check whether it is pos. or negative?
Solution:
try:
num= int(input("Enter a number:"))
if(num>0):
print("number is positive")
else:
print("number is negative")
except:
print("Please enter a number only. Thank You!")
Output: Enter a number:-1
number is negative
ques: 2] Read a number check whether it is odd or even?
Solution:
try:
num= int(input("Enter a number:"))
if(num%2==0):
print("number is even")
else:
print("number is odd")
except:
print("Please enter a number only. Thank You!")
Output:
python odd_even.py
Enter a number:100
number is even
ques: 3] Read the age of a person & display whether that person can vote or can not vote.
Solution:
try:
Age= int(input("Enter your age:"))
if(Age>=18):
print("You're eligible to vote in elections!")
elif(Age<=0):
print("Please enter the correct age. Thank You!")
else:
print("You are under 18. You cannot Vote. Better Luck next time!")
except:
print("Please enter a number only. Thank You!")
Output:
python castavote.py
Enter your age:-25
Please enter the coorect age. Thank You!
Ques 4] Read two numbers & display largest number
Solution:
try:
num1,num2 = map(int, input().split())
#map allocates the numbers at the correct position. Note that we don’t have to explicitly specify split(‘ ‘)
# because split() uses any whitespace characters as a delimiter as default.
if (num1<num2):
print(num2,'is the largest')
else:
print(num1,'is the largest')
except:
print("Please enter the correct values!")