0

I found out that this is what I am struggling the most. I know how to write a function:

     def test(a,b):
        for x in range(20):
        if x == 15:
            return
    print("test")
    test()

but I don't know how to create a function with something already written:

contacts = {"Alice": "+1554", "Bob": "+1885", "Carol": "+1006", "Felix":"+1604", "Max":"+1369","Chris":"+1882","Lea":"+1518","Linn":"+1742","Julia":"+1707"}

def phone_book(name,book):
    if name in contacts:
        return book

while True:
    print('Enter a name: (blank to quit)')
    name = input()
    if name == '':
        break
    if name in contacts:
        print(contacts[name] + ' is the number of ' + name)

I cannot bring to this code to work if I try to utilize a function. Can you explain me how to do this? You don't need to write the code. I really want to understand it.

Barmar
  • 741,623
  • 53
  • 500
  • 612
Max
  • 45
  • 4
  • 3
    I don't understand your question – NoNameAv May 23 '22 at 21:48
  • It's not very clear what you are asking for. Define a function, think about what paramters it needs to work and what it should return (if it should return anything) and then put the code in it, replace parameter names if required and finally call the function with appropriate parameters. – Mushroomator May 23 '22 at 21:49
  • Hey in the example I wrote I wanted to write a little phonebook. The latter solution I came up with relatively fast. but we are supposed to use def.. But I tried it as you said. But if I continue writing it below a function, python returns me only errors. So what I don't understand Is how can a code be correct on the one side but on the other totally false – Max May 23 '22 at 21:53
  • What is `book` supposed to be? Why do you return that instead of something from `contacts`? – Barmar May 23 '22 at 21:59
  • "I know how to write a function" but not how to format one. Please fix the indentation of your question code. – Mark Tolonen May 24 '22 at 09:44

3 Answers3

1

A function is a way that allows you to automate a certain process in your code.

In your test function, you define 2 parameters so that means you have to provide those 2 variables in order for that function to work. A simple function could be as well as

def test(a,b):
        c = a + b
        print("The sum of  number " + str(a) + " + " + str(b) + " is " + str(c))
test(5, 5)

if you pay attention to the last line I provide the function name along with those 2 variables, only that now they have values (they are now called arguments)

Hopefully the links below can give you more clarifications

https://www.w3schools.com/python/python_functions.asp

Basic explanation of python functions

SLIME SHADY
  • 26
  • 1
  • 5
1

You can have the function return the formatted string. Then the main code calls the function and prints what it returns.

def phone_book(name,book):
    if name in book:
        return f'{book[name]} is the number of {name}'
    else:
        return f'{name} is unknown'

while True:
    print('Enter a name: (blank to quit)')
    name = input()
    if name == '':
        break
    print(phone_book(name, contacts))
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Please Just structure it in a class.

class contact_book:
  def __init__(self):
    self.contacts = {"Alice": "+1554", 
                     "Bob": "+1885", 
                     "Carol": "+1006"}

  def add(self, name, value): self.contacts[name] = value
  def phone(self, name): return self.contacts[name]
  def do_i_know_you(self, name): return name in self.contacts
  def __str__(self): return f"Known contacts are: {list(self.contacts.keys())}"

Usage:

myBook = contact_book()
myBook.add('brian', '4411')
print(myBook) 
print(myBook.do_i_know_you('Martin'))

Result:

Known contacts are: ['Alice', 'Bob', 'Carol', 'brian']
False
Daniel Exxxe
  • 59
  • 1
  • 7