为不同的值和函数调用格式化打印语句



我正在练习我的python,并试图格式化match_string的以下代码的print语句,以便它将以这种格式打印:

There are 5 numbers, 4 letters and 2 other characters. 

我尝试过:

print("There are:", + x.nums, + "numbers", +x.letters,+"letter", + x.other, +"other characters")

我得到错误:

AttributeError: 'tuple' object has no attribute 'nums'

我还认为我对getDupes部分也有问题,但我不知道是什么,它只是打印出与.相同的内容

这是我的代码:

def match_string(words):
nums = 0
letter = 0
other = 0
for i in words :
if i.isalpha():
letter+=1
elif i.isdigit():
nums+=1
else:
other+=1
return nums,letter,other

def getDupes(x):
d = {}
for i in x:
if i in d:
if d[i]:
yield i
d[i] = False
else:
d[i] = True
x = match_string(input("enter a sentence"))
c = getDupes(x)
print(x)
#print("There are:", + str(x.nums), + "numbers", +x.letters,+"letter", + x.other, +"other characters")
print("PRINT",x)

funtion match_string返回元组,因此无法使用x.nums访问请尝试以下代码的

nums,letter,other = match_string(input("enter a sentence"))
print("There are:", + nums, + "numbers", + letters,+"letter", + other, +"other characters")

最新更新