Home > Software engineering >  Python using jupyter
Python using jupyter

Time:11-19

I got an assignment - Create a function called over_kmph_limit which takes two parameters:

  • mph_speed(representing the speed of a car in miles per hour)
  • kmph_limit(representing the speed limit in kilometres per hour)

The function should return True if the car is exceeding the speed limit, and False otherwise. Input values will always be single positive numerical values.

Here is what I tried:

def over_kmph_limit(mph_speed, kmph_limit):
    for x, y in range(mph_speed, kmph_limit):
        convert = x * 1.61
        if convert > y:
            Print('True')
        else:
            Print('False')
    return

I'm new to programming, so this might not be good. Another try was like below

def over_kmph_limit(mph_speed):
    for i in range(mph_speed):
        Convert = i * 1.61


    return Convert

One of the test cases will be like

over_kmph_limit(30, 40)

CodePudding user response:

You are mixing up a lot of things here. You are a beginner and you must invest your time in learning the basics of coding and then try yourself a lot.

What you have to achieve can be done simply like below

def over_kmph_limit(mph_speed, kmph_limit):
    # return if miles converted to km, is greater than the km limit
    return mph_speed*1.61 > kmph_limit

CodePudding user response:

def over_kmph_limit(mph_speed, kmph_limit):
convert = mph_speed * 1.61
if convert > kmph_limit:
    print('True')
else:
    print('False')

Thanks for the help (not sarcasm)

  • Related