Calculate speed
Write a program that takes the distance and the time to print speed in the output
Speed = to move / time
Write a program that takes the distance and the time to print speed in the output
Speed = to move / time
# Calculate speed | |
D = int(input("Distance traveled: ")) | |
T = int(input("Time: ")) | |
if D <= 0 or T <= 0: | |
print("Error!") | |
else: | |
print("Speed:",D/T) |
# Dictionary of distance units with their names | |
Distance_unit = { | |
1: "miles", | |
2: "kilometers", | |
3: "nautical miles", | |
4: "yards", | |
5: "feet", | |
6: "inches", | |
7: "millimeters", | |
8: "centimeters", | |
9: "meters" | |
} | |
# Dictionary of time units with their names | |
Time_unit = { | |
1: "hours", | |
2: "minutes", | |
3: "seconds" | |
} | |
# Input validation function | |
def get_valid_input(prompt, valid_range): | |
while True: | |
try: | |
value = int(input(prompt)) | |
if value in valid_range: | |
return value | |
print(f"Please enter a number between {min(valid_range)} and {max(valid_range)}") | |
except ValueError: | |
print("Please enter a valid number") | |
# Get distance unit input with validation | |
Distance_unit_input = get_valid_input( | |
"Enter the unit of distance you want to convert from\n" | |
"1(miles) 2(kilometers) 3(nautical miles) 4(yards)\n" | |
"5(feet) 6(inches) 7(millimeters) 8(centimeters) 9(meters): ", | |
Distance_unit.keys() | |
) | |
# Get time unit input with validation | |
Time_unit_input = get_valid_input( | |
"Enter the unit of time you want to convert from:\n" | |
"1(hours) 2(minutes) 3(seconds): ", | |
Time_unit.keys() | |
) | |
# Get distance and time values with validation | |
while True: | |
try: | |
Distance_traveled = float(input(f"Enter the distance traveled in {Distance_unit[Distance_unit_input]}: ")) | |
if Distance_traveled >= 0: | |
break | |
print("Distance must be non-negative") | |
except ValueError: | |
print("Please enter a valid number") | |
while True: | |
try: | |
Time_traveled = float(input(f"Enter the time traveled in {Time_unit[Time_unit_input]}: ")) | |
if Time_traveled > 0: | |
break | |
print("Time must be greater than zero") | |
except ValueError: | |
print("Please enter a valid number") | |
# Calculate speed | |
Speed = Distance_traveled / Time_traveled | |
# Print result with proper formatting | |
print(f"The speed is {Speed:.2f} {Distance_unit[Distance_unit_input]} per {Time_unit[Time_unit_input]}") |
Submitting answers is currently unavailable.
Scissors Paper Play: 1- The user chooses one between paper or scissors. 2- Select the system by random. 3- With the choice of user and system, the result is displayed and the user's rating is calculated 4- ... ...
Write a program that receives 2 numeric value from the input and stores in variables A and B. Then move the values of these two variables (the value of variable A is to be stored within B and the value of variable B is saved within a)
Write a program that receives a text and a word from the user, search the word in the text, and as the example below and then put the word * and print the text in the output. Example: Input1: Hello. My name is hooshang. I ...
Write a program that receives a number from the user and converts to letters suppose the numbers entered between 0 and 1000000 23 ➞ Twenty Three 405 ➞ Four HundredR
Write a program that receives a text from the user as an input and all the words used in the text, along with the number of repeating them to the user, such as: Input: This is an Example. This is a text ... ...
برای استفاده از این بخش باید وارد حساب کاربریت بشی
ورود/ثبت نام If you don’t understand the exercise or can’t solve it for any reason, that’s completely
normal—don’t worry 😊
Try checking out easier exercises and reviewing different answers
submitted by others. Gradually, you can move on to more challenging exercises. Also, your answer
might be correct even if it’s different from others.