Adsense Ad

Monday, 27 May 2019

Write a Python program which accepts the radius of a circle from the user and compute the area.


Python: Area of a Circle
In geometry, the area enclosed by a circle of radius r is πr2. Here the Greek letter π represents a constant, approximately equal to 3.14159, which is equal to the ratio of the circumference of any circle to its diameter.
Sample Solution:-
Python Code:
import math
radius = float(input("Input the radius of the circle: "))
area = math.pi * radius ** 2
print("The area of the circle with radius "+str(radius)+" is: "+str(area))

Sample Output:
Input the radius of the circle: 1.1 
The area of the circle with radius 1.1 is: 3.8013271108436504


Flowchart:

Thursday, 16 May 2019

Python: Dictionary in a dictionary with list example



Python: Dictionary in dictionary example


Python: List in dictionary example


Python: Dictionary / Tuple / User Input / While Loop. (Mark Sheet Example)

CODE: 

print("Mark sheet Program using dictionary, tuple with while loop \n")
students = {}
bio = ('Student Name','Father Name','Class')
student_counter = 0
exit = False
values = ''
while exit == False:
    student_counter += 1  
    student_details = {}
    for bio_inp in range(0,len(bio)):
        ans = input(bio[bio_inp].title()+": ")
        student_details[bio[bio_inp].title()]=ans.title()
    subList = True
    subjects = {}
    while subList:
        sub = input('Input Subject: ')
        marks = int(input("Input marks obtained out of 100: "))
        if marks < 0 or marks >100:
            print('Invalid Marks given. Must be in 1-100')
            break
        subjects[sub.title()] = marks
        again = input("\nDo you want to enter more subject? [no]: ")
        if again == '' or again.lower() == 'no':
            subList = False
    student_details['Marks'] = subjects
    students['student'+str(student_counter)]=student_details
    ask = input('\nDo you want to input marksheet record? [no]: ')
    if ask == '' or ask.lower() == 'no':
        exit = True
        print('\nData input closed. Run program again\n')
    elif ask.lower() == 'yes':
        continue
    else:
        print('\nInvalid input! Abnormal closed. Run program again\n')
        break
        
for stdkeys,stdval in students.items():
    for std_bio,bio_data in stdval.items():
        totmarks = 0
        sumMarks = 0
        percentage = 0
        if std_bio!='Marks':
            print(std_bio+" "+bio_data)
        else:
            for sub,marks in bio_data.items():
                totmarks = totmarks + 100
                sumMarks = sumMarks + marks
                print('\t'+sub+": "+str(marks))
    percentage = (sumMarks / totmarks) * 100
    print("Total marks obtained: "+str(sumMarks)+" out of "+str(totmarks))
    print("Securing percentage at: "+str(percentage)+"%")
    if percentage >=80 and percentage <=100:
        print("Grade: A+")
    elif percentage >=70 and percentage <=79:
        print("Grade: A")
    elif percentage >=60 and percentage <=69:
        print("Grade: B")
    elif percentage >=50 and percentage <=59:
        print("Grade: C")
    elif percentage >=40 and percentage <=49:
        print("Grade: D")
    else:
        print("Fail")
    print('\n')
print(students)

OUTPUT: 

Mark sheet Program using dictionary, tuple with while loop Student Name: hasan Father Name: JAWAID Class: 1 Input Subject: eng Input marks obtained out of 100: 70 Do you want to enter more subject? [no]: yEs Input Subject: urdu Input marks obtained out of 100: 60 Do you want to enter more subject? [no]: Do you want to input marksheet record? [no]: YES Student Name: ALI\ Father Name: hasan Class: 2 Input Subject: math Input marks obtained out of 100: 99 Do you want to enter more subject? [no]: yes Input Subject: sci Input marks obtained out of 100: 99 Do you want to enter more subject? [no]: Do you want to input marksheet record? [no]: Data input closed. Run program again Student Name Hasan Father Name Jawaid Class 1 Eng: 70 Urdu: 60 Total marks obtained: 130 out of 200 Securing percentage at: 65.0% Grade: B Student Name Ali Father Name Hasan Class 2 Math: 99 Sci: 99 Total marks obtained: 198 out of 200 Securing percentage at: 99.0% Grade: A+

Wednesday, 8 May 2019

Calculator in Python example



#Assignment Calculator
prompt = "Enter number "
num1 = int(input(prompt))
prompt = "To DO? '+','-','*','/' "
todo = input(prompt)
prompt = "Enter number again "
num2 = int(input(prompt))
if todo == '+':
    print(num1+num2)
elif todo == '-':
    print(num1-num2)
elif todo == '*':
    print(num1*num2)
elif todo == '/':
    print(num1/num2)  

Enter number 16
To DO? '+','-','*','/' -
Enter number again 8
Result: 8