Question

In: Computer Science

I am having a trouble with a python program. I am to create a program that...

I am having a trouble with a python program. I am to create a program that calculates the estimated hours and mintutes. Here is my code.

#!/usr/bin/env python3
#Arrival Date/Time Estimator
#
#

from datetime import datetime
import locale

mph = 0
miles = 0
def get_departure_time():
    while True:
        date_str = input("Estimated time of departure (HH:MM AM/PM): ")
        try:
            depart_time = datetime.strptime(date_str, "%H:%M %p")
        except ValueError:
            print("Invalid date format. Try again.")
            continue
        return depart_time


def get_departure_date():
    while True:
        date_str = input("Estimated date of departure (YYYY-MM-DD): ")
        try:
            depart_date = datetime.strptime(date_str, "%Y-%m-%d")
        except ValueError:
            print("Invalid date format. Try again. ")
            continue
        return depart_date
  
def travel_calculations():

      Estimated_travel_time = (miles / mph)
      a=timedelta(hours=Estimated_travel_time)
      a=str(a)
      a=a.split(':')
      print (" The estimated travel time: ",a[0],'hours and ', a[1],'minutes')

def main():
    print("Arrival Time Estimator\n")
    depart_time = get_departure_time()
    depart_date = get_departure_date()
    travel_calculations()
    miles = input("Enter miles: ")
    mph = input("Enter miles per hour : ")
    return

if __name__ == "__main__":
    main()

Solutions

Expert Solution

Created Functions distance and speed functions to get input from users along with little error handling.

And return distance and speed and store these values to miles, mph

Then pass these variables as parameters to our travel_calculations(miles, mph). In order to calculate total hours and minutes

Python program

from datetime import datetime,timedelta
import locale

mph = 0
miles = 0
def get_departure_time():
while True:
date_str = input("Estimated time of departure (HH:MM AM/PM): ")
try:
depart_time = datetime.strptime(date_str, "%H:%M %p")
except ValueError:
print("Invalid date format. Try again.")
continue
return depart_time


def get_departure_date():
while True:
date_str = input("Estimated date of departure (YYYY-MM-DD): ")
try:
depart_date = datetime.strptime(date_str, "%Y-%m-%d")
except ValueError:
print("Invalid date format. Try again. ")
continue
return depart_date

def distance():
while True:
# Get miles User input
miles = input("Enter miles to be travelled: ")
#validate
try:
miles = int(miles)
# Errorcheck
if miles <= 0:
print("Enter a Number Greater than 0.\n")
else:
return miles
except ValueError:
print("Format Error . Please Try again\n")
continue
def speed():
while True:
# Get speed User input
speed = input("Enter miles per hour: ")

try:
mph = int(speed)
# Errorcheck
if mph <= 0:
print("Enter a Number Greater than 0.\n")
else:
return mph
except ValueError:
print("Format Error . Please Try again\n")
continue
pass
  
def travel_calculations(miles,mph):
# Calculating number of hours and minutes
hours = int(miles/mph)
minutes = int(((miles/mph)-hours)*60)
time_travelled = timedelta(hours=hours, minutes=minutes)
  
# print our result
print("\n\n******Time*******")
print("Hours: ", hours)
print("Minutes: ", minutes)


def main():
print("Arrival Time Estimator\n")
depart_time = get_departure_time()
depart_date = get_departure_date()
  
miles = distance()
mph = speed()
travel_calculations(miles,mph)
return

if __name__ == "__main__":
main()

Output


Related Solutions

I am currently having trouble understanding/finding the errors in this python code. I was told that...
I am currently having trouble understanding/finding the errors in this python code. I was told that there are 5 errors to fix. Code: #!/usr/bin/env python3 choice = "y" while choice == "y": # get monthly investment monthly_investment = float(input(f"Enter monthly investment (0-1000):\t")) if not(monthly_investment > 0 and monthly_investment <= 100): print(f"Entry must be greater than 0 and less than or equal to 1000. " "Please start over.")) #Error 1 extra ")" continue # get yearly interest rate yearly_interest_rate = float(input(f"Enter...
Hello! I am having trouble starting this program in Java. the objective is as follows: "...
Hello! I am having trouble starting this program in Java. the objective is as follows: " I will include a text file with this assignment. It is a text version of this assignment. Write a program that will read the file line by line, and break each line into an array of words using the tokenize method in the String class. Count how many words are in the file and print out that number. " my question is, how do...
I was able to calculate (a) but I am having trouble with the calculations of (b)....
I was able to calculate (a) but I am having trouble with the calculations of (b). Thanks! The following are New York closing rates for A$/US$ and $/£:                                     A$/$ = 1.5150;               $/£ = $1.2950             (a) Calculate the cross rate for £ in terms of A$ (A$/£).             (b) If £ is trading at A$1.95/£ in London (cross market) on the same day, is there an arbitrage opportunity?  If so, show how arbitrageurs with £ could profit from this opportunity and calculate the arbitrage...
I am working through this solution in rstudio and am having trouble fitting this table into...
I am working through this solution in rstudio and am having trouble fitting this table into a linear regression analysis. an answer with corrosponding r code used would be greatly appreciated A study was conducted to determine whether the final grade of a student in an introductory psychology course is linearly related to his or her performance on the verbal ability test administered before college entrance. The verbal scores and final grades for all 1010 students in the class are...
I am having the most trouble with 1d: 1. a. Prove that if f : A...
I am having the most trouble with 1d: 1. a. Prove that if f : A → B, g : B → C, and g ◦f : A → C is a 1-to-1 surjection, then f is 1-to-1 and g is a surjection. Proof. b. Prove that if f : A → B, g : B → C, g ◦ f : A → C is a 1-to-1 surjection, and g is 1-to-1, then f is a surjection. Proof. c....
I am working on these study questions and am having trouble understanding how it all works...
I am working on these study questions and am having trouble understanding how it all works together. Any help would be greatly appreciated!! An all equity firm is expected to generate perpetual EBIT of $50 million per year forever. The corporate tax rate is 0% in a fantasy no tax world. The firm has an unlevered (asset or EV) Beta of 1.0. The risk-free rate is 5% and the market risk premium is 6%. The number of outstanding shares is...
Using dev c++ I'm having trouble with classes. I think the part that I am not...
Using dev c++ I'm having trouble with classes. I think the part that I am not understanding is sending data between files and also using bool data. I've been working on this program for a long time with many errors but now I've thrown in my hat to ask for outside help. Here is the homework that has given me so many issues: The [REDACTED] Phone Store needs a program to compute phone charges for some phones sold in the...
Hi I am having the following problem. At the moment I am trying to create a...
Hi I am having the following problem. At the moment I am trying to create a bode plot for the following function. G(s)=(Ks+3)/((s+2)(s+3)) Note: Not K(s+2)! I then want to plot multiple bode plots for various values of K. Eg. 1,2,3, etc. I am having two separate issues. 1. How do I define the TF with a constant K in the location required (a multiple of s in the numerator) 2. How do I create multiple bode plots for values...
I am having trouble with a homework question The financial statements of P&G are presented in...
I am having trouble with a homework question The financial statements of P&G are presented in Appendix B. The company’s complete annual report, including the notes to the financial statements, is available online. Click here to view Appendix B Refer to P&G’s financial statements and accompanying notes to answer the following questions. Part 1 New attempt is in progress. Some of the new entries may impact the last attempt grading.Your answer is partially correct. Under P&G’s stock-based compensation plan, stock...
Hi, I am having trouble with trying to calculate asymptotic time complexity for each of these...
Hi, I am having trouble with trying to calculate asymptotic time complexity for each of these functions. A step by step tutorial would be great. This is done in Python. Please not only the answer, but how you calculated it as well. Here is the code #Q2 # to denote ayymptotic time complexity use the following notation # O(l) O(m) O(a) O(b) # e.g. traversing through l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is O(l)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT