Python Forum
Whats the difference between these two?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Whats the difference between these two?
#1
here's my question:-
You are driving a little too fast, and a police officer stops you. Write a function to return one of 3 possible results: "No ticket", "Small ticket", or "Big Ticket". If your speed is 60 or less, the result is "No Ticket". If speed is between 61 and 80 inclusive, the result is "Small Ticket". If speed is 81 or more, the result is "Big Ticket". Unless it is your birthday (encoded as a boolean value in the parameters of the function) -- on your birthday, your speed can be 5 higher in all cases.
my answer was something like this
def caught_speeding(speed, is_birthday):
    if is_birthday:
        speeding=speed-5
    else: speeding = speed
    if speeding>80:
        print('big ticket time')
    elif speeding>60:
        print('small ticket')
    else: return ('no ticket')
solution was something like this
def caught_speeding(speed, is_birthday):
    
    if is_birthday:
        speeding = speed - 5
    else:
        speeding = speed
    
    if speeding > 80:
        return 'Big Ticket'
    elif speeding > 60:
        return 'Small Ticket'
    else:
        return 'No Ticket'
i got a similar answer so basically i wanna know if I should stick to return or is print just fine (or did i just get lucky in this scenario)
Reply
#2
If you print, you don't have the result in a str.
Other functions can call caught_speeding and work with the return value.

You could use Enum to keep your state together in a class.

If you need to change the text, then you do it only at one place.
If you have for example more functions, which are returning "Big Ticket" etc., then you repeat yourself.
In this case a change must be done at many places.


from enum import Enum


class Ticket(Enum):
    no_ticket = 'No Ticket'
    small_ticket = 'Small Ticket'
    big_ticket = 'Big Ticket'


def caught_speeding(speed):
    if speed> 80:
        return Ticket.big_ticket 
    elif speed> 60:
        return Ticket.small_ticket 
    else:
        return Ticket.no_ticket


result = caught_speeding(81)
print(result)
print(result.value)
Output:
Ticket.big_ticket Big Ticket
You can also add documentation to your Ticket class:
class Ticket(Enum):
    """
    The different states of a ticket you get from police.
    """
    no_ticket = 'No Ticket'
    small_ticket = 'Small Ticket'
    big_ticket = 'Big Ticket'


help(Ticket)
Output:
Help on class Ticket in module __main__: class Ticket(enum.Enum) | Ticket(value, names=None, *, module=None, qualname=None, type=None, start=1) | | The different states of a ticket you get from police. | | Method resolution order: | Ticket | enum.Enum | builtins.object | | Data and other attributes defined here: | | big_ticket = <Ticket.big_ticket: 'Big Ticket'> | | no_ticket = <Ticket.no_ticket: 'No Ticket'> | | small_ticket = <Ticket.small_ticket: 'Small Ticket'> | | ---------------------------------------------------------------------- | Data descriptors inherited from enum.Enum: | | name | The name of the Enum member. | | value | The value of the Enum member. | | ---------------------------------------------------------------------- | Readonly properties inherited from enum.EnumMeta: | | __members__ | Returns a mapping of member name->value. | | This mapping lists all enum members, including aliases. Note that this | is a read-only view of the internal mapping.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Try calling each function with this code and note the difference in output. an you figure out why it is different?
for speed in range(50,100,20):
	print(f'speed = {speed} : {caught_speeding(speed, False)}')
Reply
#4
General rule - a function should do one thing and do it well. In this case it should return the string to the main program and let the program control the output formatting, etc. Functions should not print unless printing is the actual purpose of the function.
Reply
#5
A simple example will show the difference. Returning a value means a variable can be instantiated or the value can be used by other functions. Printing merely prints to the standard output.

def return_number():
    return 5

def print_number():
    print(5)

x = return_number()
print(x) # x is 5 because there is a return
y = print_number()
print(y) # y is None because there is no return
As advised above, functions should have returns instead of printing. A function that prints can only print. Whereas, a function that returns can be used for nearly anything.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Whats the problem here? Class Ronaldx 4 2,212 Nov-26-2019, 10:29 PM
Last Post: jefsummers
  whats wrong with my code? syntax errors r6lay 5 6,500 Mar-16-2017, 04:14 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020