0

I'm trying to create and play with classes in Python 3.6 but I'm getting the following error when I try to call a method that prints info about my class:

'TypeError: not enough arguments for format string'

And here's the code I'm trying to run:

class Restaurant(object):

    def __init__(self, name, type):
        self.name = name
        self.type = type

    def describe(self):
        print("Restaurant name: %s , Cuisine type: %s" % self.name, self.type)

    def open_restaurant(self):
        print("%s is now open!" % self.name)


my_restaurant = Restaurant("Domino", "Pizza")

my_restaurant.describe()
my_restaurant.open_restaurant()  

The open_restaurant() method works fine but with my_restaurant.describe() I receive the error message I mentioned.

2
  • 1
    The syntax for having multiple arguments after % is % (self.name, self.type) Commented Feb 2, 2018 at 13:09
  • good answers already given. But please do not use type as a variable name. Commented Feb 2, 2018 at 13:26

3 Answers 3

2

Seeing as you are using python3.6. f-strings are much nicer.

class Restaurant(object):


    def __init__(self, name, type):
        self.name = name
        self.type = type

    def describe(self):
        print(f"Restaurant name: {self.name}, Cuisine type: {self.type}")

    def open_restaurant(self):
        print(f"{self.name} is now open!")

my_restaurant = Restaurant("Domino", "Pizza")

my_restaurant.describe()
my_restaurant.open_restaurant()  

Restaurant name: Domino, Cuisine type: Pizza
Domino is now open!
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed much more elegant! Thanks for the quick answer.
2

This works (note the tuple to the right of "%")

print("Restaurant name: %s , Cuisine type: %s" % (self.name, self.type))

You might consider the newer form

print("Restaurant name: {} , Cuisine type: {}".format(self.name, self.type))

Comments

1

You need to pass a tuple:

print("Restaurant name: %s , Cuisine type: %s" % (self.name, self.type))

But really, the % type of string formatting has been mostly obsolete in Python for many years, and you probably should not be using it in Python 3.6. Instead: try this:

print("Restaurant name: {} , Cuisine type: {}".format(self.name, self.type))

Or of course:

print("Restaurant name:", self.name, ", Cuisine type:", self.type)

6 Comments

This is bad advice. Don't do this: print("Restaurant name:", self.name, ", Cuisine type:", self.type), it's so easy to make mistakes (like you have, there is an extra space before the comma). In python 3.6 you should be using f-strings (or .format for setting up a string template).
Worked! Thank you very much
@FHTMitchell I think I will use print(f"Restaurant name: {self.name} Cuisine type: {self.type}")
Upvoting despite the bad last suggestion, if only because it addresses the problem in the question in addition to suggesting the newer format method.
@FHTMitchell: You claim I have added an extra space before the comma. But you will see that "extra" space is not extra at all, it is present in the original code from the question, and is not an accident (at least, not on my part, I was only replicating what the question writer did).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.