In: Computer Science
all python
Question One [2 * 2.5]
•The sum
•The difference
•The product
•The average
•The distance (absolute value of the difference)
•The maximum (the larger of the two)
•The minimum (the smaller of the two)
Hint: Python defines max and min functions that accept a sequence of values, each separated with a comma.
Example:
Enter a letter grade: B-
The numeric value is 2.7.
Grade = 99
if Grade>= 90: print("A") if Grade >=80 : print("B") if Grade >=70 : print("C") if Grade >=60: print("D") else: print("Failed")
1.
def sum(x, y):
return x + y
# This function subtracts two numbers
def diff(x, y):
return x - y
# This function multiplies two numbers
def product(x, y):
return x * y
# This function find average of two numbers
def average(x, y):
return x + y/2
# This function find absolue distance of two numbers
def distance(x, y):
return abs(x-y)
# This function find maximum of two numbers
def maximum(x, y):
return max(x,y)
# This function find minimum of two numbers
def minimum(x, y):
return min(x,y)
print("Select operation.")
print("1.Sum")
print("2.Difference")
print("3.Product")
print("4.Average")
print("5.Distance")
print("6.Maximum")
print("7.Minimum")
while True:
# Take input from the user
choice = input("Enter your choice: ")
# Check if choice is one of the seven options
if choice in ('1', '2', '3', '4', '5', '6', '7'):
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
if choice == '1':
print(num1, "+", num2, "=", sum(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", diff(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", product(num1, num2))
elif choice == '4':
print(num1, "+", num2, "/2 is", average(num1, num2))
elif choice == '5':
print("Absolute distance of" , num1,"and", num2, "is",
distance(num1, num2))
elif choice == '6':
print("Maximum of" , num1,"and", num2, "is", maximum(num1,
num2))
elif choice == '7':
print("Minimum of" , num1,"and", num2, "is", minimum(num1,
num2))
break
else:
print("Invalid Input")
OUTPUT:
Screenshot of the code:
2. The screenshot is:
code:
d_meter = int(input("Input distance in meter: "))
d_inches = d_meter * 39.3700787
d_feets = d_meter / 0.3048
d_miles = d_meter * 0.00062137119224
print("The distance in inches is %i inches." % d_inches)
print("The distance in feets is %.2f feets." % d_feets)
print("The distance in miles is %.2f miles." % d_miles)
Note that:
3.code:
a=input('enter the first integer:') //taking input from the user and store
it in the variable a
b=input('enter the second integer:') //taking input from the user and store
it in the variable b
c=input('enter the third integer:') //taking input from the user and store
it in the variable c
if a==b and b==c: print('all the same') //checking the condition
if not a==b and not b==c: print('all different') //checking the condition
if a==b or b==c or c==a: print('neither') //checking the condition
OUTPUT:
4.code:
grade=input("enter the grade \n")
if grade=="A":
print("4")
elif grade=='B':
print("3")
elif grade=='C':
print("2")
elif grade=='D':
print("1")
elif grade=='E':
print("0")
elif grade=='A+':
print("4")
elif grade=='B+':
print("3.3")
elif grade=='C+':
print("2.3")
elif grade=='D+':
print("1.3")
elif grade=='E+':
print("0.3")
elif grade=='A-':
print("3.7")
elif grade=='B-':
print("2.7")
elif grade=='C-':
print("1.7")
elif grade=='D-':
print("0.7")
elif grade=='E-':
print("-0.3")
else:
print("invalid grade")
OUTPUT:
5.Corrected code is
Grade=99
if Grade>= 90:
print("A")
elif Grade >=80 :
print("B")
elif Grade >=70 :
print("C")
elif Grade >=60:
print("D")
else:
print("Failed")
use elif or else if instead of if you were used in the code
upvote pls...