Python If Condition Programs | Set 7

Set 1    Set 2    Set 3    Set 4   Set 5  Set 6  Set 7  Set 8  Set 9

Question: 31

Write a python script to take input for two numbers and an operator ( +, -, * ,/ ). Based on the entered operator calculate and display the result?

Sol:

a=float(input("Enter 1st no "))
b=float(input("Enter 2nd no "))
op=input("Enter the operator (+,-,*,/) ")
if(op=="+"):
    c=a+b
    print("Sum = ",c)
elif(op=="-"):
    if(a>b):
        c=a-b
    else:
        c=b-a
    print("Diff =",c)
elif(op==""):     
    c=a*b
    print("prod = ",c)
elif(op=="/"):
    c=a/b
    print("div = ",c)
else:
    print("Invalid operator")

Output:

Enter 1st no 10
Enter 2nd no 20
Enter the operator (+,-,*,/) +
Sum = 30.0
>>>
Enter 1st no 10
Enter 2nd no 25
Enter the operator (+,-,*,/) *
prod = 250.0
>>>
Enter 1st no 23
Enter 2nd no 6
Enter the operator (+,-,*,/) –
Diff = 17.0
>>>
Enter 1st no 25
Enter 2nd no 6
Enter the operator (+,-,*,/) /
div = 4.166666666666667
>>>
Enter 1st no 12
Enter 2nd no 52
Enter the operator (+,-,*,/) )
Invalid operator
>>>

Question: 32

Write a python script to take input for day number and print the day?
example:
if input is          output
1                           Sun
2                           Mon
3                           Tue
4                           Wed
5                          Thru
6                          Fri
7                          Sat

Sol:

d=int(input("Enter days no (1..7)"))
if(d==1):
    print("Sun")
elif(d==2):
    print("Mon")
elif(d==3):
    print("Tue")
elif(d==4):
    print("Wed")
elif(d==5):
    print("Thru")
elif(d==6):
    print("Fri")
elif(d==7):
    print("Sat")
else:
    print("Invalid day number")

Output:

Enter days no (1..7) 1
Sun
>>>
Enter days no (1..7) 2
Mon
>>>
Enter days no (1..7) 7
Sat
>>>
Enter days no (1..7) 9
Invalid day number
>>>

Question: 33

Write a python script to take input for month number and print the month?
example:
if input is     output
1                     Jan
2                     Feb
3                     Mar
4                     Apr
5                    May
6                    Jun
7                    Jul
8                    Aug
9                   Sep
10                 Oct

Sol:

m=int(input("Enter month no (1..12)"))
if(m==1):
    print("Jan")
elif(m==2):
    print("Feb")
elif(m==3):
    print("Mar")
elif(m==4):
    print("Apr")
elif(m==5):
    print("May")
elif(m==6):
    print("Jun")
elif(m==7):
    print("Jul")
elif(m==8):
    print("Aug")
elif(m==9):
    print("Sep")
elif(m==10):
    print("Oct")
elif(m==11):
    print("Nov")
elif(m==12):
    print("Dec")
else:
    print("Invalid month number")

Output:

Enter month no (1..12)2
Feb
>>>
Enter month no (1..12)1
Jan
>>>
Enter month no (1..12)10
Oct
>>>
Enter month no (1..12)15
Invalid month number
>>>

Question: 34

Write a python script to take input for student details like roll, name, marks of three subjects (m1,m2,m3). calculate and print total and per. Also print the grade on the basis of given conditions:
condition                                   Grade
if(per>=90)                                   A+
if(per>=80 and per<90)              A
if(per>=70 and per<80)              B+
if(per>=60 and per<70)              B
if(per>=50 and per<60)              C
if(per>=40 and per<50)              D
if(per<40)                                      F

roll=int(input("Enter roll no "))
name=input("Enter name ")
m1=float(input("Enter marks of m1 "))
m2=float(input("Enter marks of m2 "))
m3=float(input("Enter marks of m3 "))
total=m1+m2+m3
per=total/3
if(per>=90):
    grade="A+"
elif(per>=80 and per<90):
    grade="A"
elif(per>=70 and per<80):
    grade="B+"
elif(per>=60 and per<70):
    grade="B"
elif(per>=50 and per<60):
    grade="C"
elif(per>=40 and per<50):
    grade="D"
else:
    grade="F"
print("Total ",total," Per ",per," Grade ",grade)

Output:

Enter roll no 101
Enter name abc
Enter marks of m1 98
Enter marks of m2 95
Enter marks of m3 97
Total 290.0 Per 96.66666666666667 Grade A+
>>>

Set 1    Set 2    Set 3    Set 4   Set 5  Set 6  Set 7  Set 8  Set 9