我想創建一個簡單的腳本,應該計算列表中有多少個integers
或strings
。 起初列表爲空,則該用戶被要求用數字或字符串來填補它,這是腳本:Python列表讀取整數
lis = [] # list name
num, lett = 0, 0 # init counters of numbers and letters
while True:
x = input("digit an int or string, 'stop' returns values: ")
if(x=='stop'):
False
break
i = lis.append(x)
if isinstance(i, int): # check whether is integer
num += 1
else:
lett += 1
print(lis)
print("there are " + str(num) + " numbers")
print("there are " + str(lett) + " strings")
該項目工程,但問題就來了,最終,因爲當我打印列表,它只能看到字符串,偶數例如返回爲'10'。
我需要解釋器來自動識別整數。
你需要使用'X .isdigit()',因爲'x'仍然是一個字符串'isinstance()'每次都會失敗,因爲它測試變量的數據類型而不是_character content_。但是除了這個之外,你的代碼中還有很多更多的錯誤。我建議閱讀Python的教程,以瞭解如何在其中進行編程。 –
''程序工作''我非常懷疑這一點。 ''lis.append(x)''returns''None''。順便說一句:在「break」之前的「False」什麼都不做。 –
**正在回答**的用戶:請停止回答此問題。它是廣泛的,無關緊要的,很可能不會對未來的用戶有所幫助。如果他閱讀一個Python的begginer教程,它將使OP受益匪淺。在代碼轉儲完成後,只需發佈代碼轉儲無助於任何人。至少你可以提供你的代碼的解釋。 –