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...
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...
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...
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
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...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute the sum of the numbers between these two numbers (including the numbers entered). If the user Enters 2 and 6. The program will calculate the sum of the numbers: 2+3+4+5+6 and will display the result. Enter First number> 2 Enter second number> 6 The sum is 20
(using for loop, parameters, if, else condition only )Python: Write a program that prompts the user...
(using for loop, parameters, if, else condition only )Python: Write a program that prompts the user to enter a positive integer value, and compute the following sequence: • If the value is even, halve it. • If it's odd, multiply by 3 and add 1. • Repeat this process until the value is 1, printing out each value. • Then print out how many of these operations you performed. If the input value is less than 1, print a message...
Write a Python program using functions and mainline logic which prompts the user to enter a...
Write a Python program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a file. It should then display the following data to back to the user: The list of integers The lowest number in the list The highest number in the list The total sum of all the numbers in the list The average number in the list At a minimum, the numbers should...
Write a PYTHON program that prompts the user to enter the account’s present value, yearly interest...
Write a PYTHON program that prompts the user to enter the account’s present value, yearly interest rate, and the number of years that the money will be left in the account. The program should pass these values to a function that returns the future value of the account, after the specified number of months. The program should display the accounts’ future value
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT