Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

The program is supposed to take in two names, and if they are the same length it should check if they are the same word. If it's the same word it will print "The names are the same". If they are the same length but with different letters it will print "The names are different but the same length". The part I'm having a problem with is in the bottom 4 lines.

#!/usr/bin/env python 

# Enter your code for "What's In (The Length Of) A Name?" here. name1 = input("Enter name 1: ") 

name2 = input("Enter name 2: ") 

len(name1)

len(name2) 

if len(name1) == len(name2):

if name1 == name2: 

  print ("The names are the same") 

else: 

  print ("The names are different, but are the same length") 

if len(name1) > len(name2): 

  print ("'{0}' is longer than '{1}'"% name1, name2) 

elif len(name1) < len(name2): 

  print ("'{0}'is longer than '{1}'"% name2, name1)

When I run this code it displays:

Traceback (most recent call last): 

 File "program.py", line 13, in <module> 

   print ("'{0}' is longer than '{1}'"% name1, name2)

TypeError: not all arguments converted during string formatting

Any suggestions are highly appreciated.

1 Answer

0 votes
by (106k points)
edited by

The error you are getting is because you are doing wrong string formatting. So, the correct way to use traditional string formatting using the '%' operator is to use a printf-style format string see the syntax below:- 

"'%s' is longer than '%s'" % (name1, name2)

To know more about this you can have a look at the following video tutorial:-

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...