簡體   English   中英

為不同的值和函數調用格式化打印語句

[英]formatting print statement for different values and function call

我正在練習我的 python 並嘗試為 match_string 格式化以下代碼的打印語句,以便它將以這種格式打印:

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)

函數 match_string 返回一個元組,因此不能通過使用 x.nums 來訪問,而是嘗試下面的代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM