该程序应该接受两个名称,如果它们的长度相同,它应该检查它们是否是同一个单词。如果是同一个词,它将打印"The names are the same"。如果它们的长度相同但字母不同,它将打印"The names are different but the same length"。我遇到问题的部分在底部的 4 行中。
#!/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)
当我运行此代码时,它显示:
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
任何建议都受到高度赞赏。