Question

In: Computer Science

all python Question One [2 * 2.5] Write a program that prompts the user for two...

all python

Question One [2 * 2.5]

  1. Write a program that prompts the user for two integers and then prints

•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.

  1. Write a program that prompts the user for a measurement in meters and then converts it to miles, feet, and inches.

  1. Write a program that reads three numbers and prints “all the same” if they are all the same, “all different” if they are all different, and “neither” otherwise.

  1. Write a program that translates a letter grade into a number grade. Letter grades are A, B, C, D, and F, possibly followed by + or –. Their numeric values are 4, 3, 2, 1, and 0. There is no F+ or F–. A + increases the numeric value by 0.3, a – decreases it by 0.3. However, an A+ has value 4.0.

Example:

Enter a letter grade: B-

The numeric value is 2.7.

  1. Test and Correct and the following program:                 
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")

Solutions

Expert Solution

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:

  • 1 Meter (m) is equal to 39.3700787 inches. To convert meters to inches, multiply the meter value by 39.3700787.
  • 1 Meter is equal to 3.28084 feet
  • 1 meter is equal to 0.000621371192 mile

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...


Related Solutions

Question: Write a program in python that reads in the file climate_data_2017_numeric.csv and prompts the user...
Question: Write a program in python that reads in the file climate_data_2017_numeric.csv and prompts the user to enter the name of a field (other than Date), and then outputs the highest and lowest values recorded in that field for the month of August. The file climate_data_2017_numeric.csv contains the following fields: Date Minimum temperature (C) Maximum temperature (C) Rainfall (mm) Speed of maximum wind gust (km/h) 9am Temperature (C) 9am relative humidity (%) 3pm Temperature (C) 3pm relative humidity (%) Expected...
Write for Python IDLE 3.8.5 Write functions: i) One that prompts a user for 2 numbers....
Write for Python IDLE 3.8.5 Write functions: i) One that prompts a user for 2 numbers. ii) Adds the two numbers if they are even iii) Multiplies the two numbers if they are odd iv) Displays the appropriate output to the user You are writing 4 functions and calling them to test functionality.
In Python write a program that prompts the user for a file name, make sure the...
In Python write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try...
Write a Python program that prompts the user to enter a list of words and stores...
Write a Python program that prompts the user to enter a list of words and stores in a list only those words whose first letter occurs again within the word (for example, 'Baboon'). The program should display the resulting list..................please explain step by step
Write a program function code in Python that prompts the user to enter the total of...
Write a program function code in Python that prompts the user to enter the total of his/her purchase and add 5% as the VAT. The program should display the total without and with VAT. ( that mean i should ask the user to enter the number of their item " so i think that i should use range" then print the result with and without the VAT )
Write a Python program that prompts the user for a dollar amount to place in savings,...
Write a Python program that prompts the user for a dollar amount to place in savings, the amount of interest per year, and the amount of years you decide to keep it there. The program will calculate how much money you will have each year. Use loops in your calculations and Use the loop below: for year = 1 to X savings = savings * interestRate
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program...
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program should display a letter grade for each score and the average test score. Hint: Declare local variables under main() program Prompts the user to enter 5 test scores Define a function to calculate the average score: this should accept 5 test scores as argument and return the avg Define a function to determine the letter grade: this should accept a test score as argument...
C++ Question: write a program that prompts the user for the length and width of a...
C++ Question: write a program that prompts the user for the length and width of a rectangle in inches.  The program then uses functions to compute the perimeter and area of the rectangle and to convert those to meters and square meters respectively. Sample output from one instance of the program is shown below: ```html Welcome to the Foot-To-Meter Rectangle Calculator ================================================= Enter the rectangle length in feet: 2 Enter the rectangle width in feet: 3 The rectangle dimensions are: 0.61...
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter...
Write a program In python of Jupiter notebook (using loops) that prompts the user to enter his/her favorite English saying, then counts the number of vowels in that (note that the user may type the saying using any combination of upper or lower case letters). Example: Enter your favorite English saying: Actions speak LOUDER than words. Number of wovels: 10
Write a Python program which prompts the user to input a string. Then, print the string...
Write a Python program which prompts the user to input a string. Then, print the string in reverse to the terminal Sample output Please enter a word: "zeus" The reverse of zeus is suez Hint: There are several ways to accomplish this. Recall that a string is an itterable object and therefore can be used with a for loop
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT