Fundamentals of Computer Programming(Apply computer programming techniques to solve real world problems by Python)
Question #1:
Write a python program that takes
balance of a user’s account as input. It should then ask the user how much
amount he\she wants to withdraw from his\her account. The program should take
this amount as input and deduct from the balance. Similarly, it should ask the user
how much amount he\she wants to deposit in his\her account. It should take this
amount as input and add to the balance. The program shall display the new
balance after amount has been withdrawn and deposited.
Note:
Your program should have all necessary checks on the transactions. Display a
menu to the user to let him\her choose between different available options.
Solution:
Copy of Source Code:
#program to calculate current balance after withdrawal
and deposit
balance=get=w=d=0 #variables required
yn="y"
balance=eval(input("Enter your account balance:
")) #input balance
#menu for the withdrawal
print("For the withdrawal")
print(" 1 is to withdraw Rs.500")
print(" 2 is to withdraw Rs.1000")
print(" 3 is to withdraw Rs.1500")
print(" 4 is to withdraw Rs.2000")
get=eval(input("Enter the number corresponding to
the amount you want to withdraw: ")) #input get
#if statement to decide value of w
if get==1 :
w=500
elif get==2:
w=1000
elif get==3:
w=1500
elif get==4:
w=2000
else:
print("\nInvalid
Input. Proceeding to deposit.\n") #displayed when wrong input
if w>balance:
#program
course of action when withdrawal is more than balance
print("You can not withdraw more than your balance.")
yn=input("If you wish to proceed to deposit, enter Y if not then
enter N: ")
#choice to
user if he/she wants to still deposit or not
if
yn=="Y":
print("For the deposit")
print(" 1 is to deposit Rs.500")
print(" 2 is to deposit Rs.1000")
print(" 3 is to deposit Rs.1500")
print(" 4 is to deposit Rs.2000")
get=eval(input("Enter the number corresponding to the amount you
want to deposit: "))
if get==1:
d=500
elif
get==2:
d=1000
elif
get==3:
d=1500
elif get==4:
d=2000
else:
print("Invalid Input.")
input("Press Enter to exit.")
balance=balance+d
print("Your current balance is", balance)
else:
print("Exit.")
else:
#program
course of action when withdrawal not less than deposit
balance=balance-w
print("For the deposit")
print("
1 is to deposit Rs.500")
print("
2 is to deposit Rs.1000")
print("
3 is to deposit Rs.1500")
print("
4 is to deposit Rs.2000")
get=eval(input("Enter the number corresponding to the amount you
want to deposit: "))
if get==1:
d=500
elif get==2:
d=1000
elif get==3:
d=1500
elif get==4:
d=2000
else:
print("Invalid
Input.")
input("Press Enter to exit.")
#final value
of balance
balance=balance+d
print("Your current balance is", balance) #to display balance
input("Press Enter to exit.")
Question #2:
Write a Python code:
1.
To take the
marks of 5 subjects from the user and store them in different variables.
2.
Find the average
of the marks.
3.
Decide the grade
based on the average of them marks.
Solution:
Copy of Source Code:
# program to display grade based on average of 5 marks
from user
eng=urd=math=sci=comp=1
print ("Enter marks less than or equal to 100 for
the following subjects:-")
eng=eval(input("English: ")) #this gets the
marks for english
urd=eval(input("Urdu: ")) #this gets the
marks for urdu
math=eval(input("Maths: ")) #this gets the
marks for maths
sci=eval(input("Science: ")) #this gets the
marks for science
comp=eval(input("Computer: ")) #this gets
the marks for compputer
# if statement to ensure all marks are not greater
than 100
if(urd>100 or eng>100 or math>100 or
comp>100 or sci>100):
print("Wrong input.")
else: # if input is correct, the following calculation
will be done
avg=(eng+urd+comp+sci+math)/5
#the
decision statement displays grade according to the following criteria of
average
#A if
>=90
#B if
>=80
#C if
>=70
#D if
>=60
#E if
>=50
#F if <
50
if
avg>=90:
print("Your grade is A")
elif
avg>=80:
print("Your
grade is B")
elif
avg>=70:
print("Your grade is C")
elif
avg>=60:
print("Your grade is D")
elif
avg>=50:
print("Your grade is E")
else:
print("Your grade is F") #this displays F if none of the above
conditions are met
input("Press enter to exit:") #for the
output to pause until user presses enter
Question #3:
Describe the strengths and
weaknesses of python as a modern programming language while highlighting its
dominant application areas (300-500).
Solution:
Python is undoubtedly the
most popular programming language in the world as of right now. That is because
there are countless advantages of coding in python but there are also some
areas where python exhibits weakness.
To discuss the strengths of
python, the most important reason why it is popular is because it is easy
to learn. Beginners can code various things without knowing the
specifics such as memory references and all the low-end details. Python offers
the programmers access to a large number of modules and ready
constructs. Consequently, the programmer can focus on the logic of the program
and the task at hand without using up a lot of time on writing the code which
provides increased productivity. There are a lot of online resources
available for use when it comes to python. These include libraries for machine
learning, artificial intelligence (AI) and a lot of other fields. One code
written in python can be used anywhere i.e., Mac, Windows, Linux, Unix. Python
is also open source and free of cost. It is public to modify and redistribute.
Python is interpreted so this line-by-line execution provides ease of
debugging. All of these specifications make python a strong modern
programming language.
Although it has many
advantages, python can also be disadvantageous in some regards. Python is
interpreted and executed line by line. This causes it to be relatively slow
as compared to other languages like C/C++ and low-level languages. For this
purpose, it is not as efficient when it comes to 3D rendering in game
development. Other than this, python is weak in the field of mobile
computing. Python also needs some more testing time. Some bugs are only
detectable during run time. As python is flexible when it comes to data types,
the memory consumption is high so it is not a good choice for memory
intensive tasks. In addition to this, the libraries available for python are not
competent with other programming languages. Most programming languages have
a strong syntax but python does not. This results in difficulty while translating
a program into another programming language.
Being a popular and strong
language, python is used in many fields and its application areas are
considerably increasing. It is a leading choice when it comes to developing web
applications. Instagram and Dropbox were originally built on python. It is
also used for creating Graphical User Interfaces (GUI) and desktop
applications as well as for developing games. Python is the leading
choice when it comes to the financial industry. Networking apps, database
applications and scripting are also fields in which python is
widely used. Python is used in machine learning and Artificial
Intelligence (AI) via pre-built libraries and resources.
In conclusion, python is a
strong and dominant programming language in the modern day and age. Besides its
few weaknesses, it has numerous strengths that overpower them greatly. The ease
and efficiency of python makes it the most used and widely renowned programming
language today.
Comments
Post a Comment