2017-04-22 95 views
0

我想創建一個簡單的腳本,應該計算列表中有多少個integersstrings。 起初列表爲空,則該用戶被要求用數字或字符串來填補它,這是腳本: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'。

我需要解釋器來自動識別整數。

+0

你需要使用'X .isdigit()',因爲'x'仍然是一個字符串'isinstance()'每次都會失敗,因爲它測試變量的數據類型而不是_character content_。但是除了這個之外,你的代碼中還有很多更多的錯誤。我建議閱讀Python的教程,以瞭解如何在其中進行編程。 –

+0

''程序工作''我非常懷疑這一點。 ''lis.append(x)''returns''None''。順便說一句:在「break」之前的「False」什麼都不做。 –

+0

**正在回答**的用戶:請停止回答此問題。它是廣泛的,無關緊要的,很可能不會對未來的用戶有所幫助。如果他閱讀一個Python的begginer教程,它將使OP受益匪淺。在代碼轉儲完成後,只需發佈​​代碼轉儲無助於任何人。至少你可以提供你的代碼的解釋。 –

回答

0
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'): 
    break 
    if x.isdigit(): 
    lis.append(int(x)) 
    num += 1 
    elif x.isalpha(): 
    lis.append(x) 
    lett += 1 

print(lis) 
print("there are " + str(num) + " numbers") 
print("there are " + str(lett) + " strings") 

結果

digit an int or string, 'stop' returns values: 1 
digit an int or string, 'stop' returns values: 2 
digit an int or string, 'stop' returns values: 3 
digit an int or string, 'stop' returns values: a 
digit an int or string, 'stop' returns values: b 
digit an int or string, 'stop' returns values: c 
digit an int or string, 'stop' returns values: stop 
[1, 2, 3, 'a', 'b', 'c'] 
there are 3 numbers 
there are 3 strings 
0
d = l = 0 
res = [] 
while True: 
    s = input("input string or digit\n") 
    if s == 'exit': 
     break 

    ## this is one way, might be faster to do it using isdigit suggested in the comment 
    try:    
     temp = int(s) 
     d += 1 
    except ValueError: 
     l += 1 
    res.append(s) 

print(d) 
print(l) 
print(res) 
+0

使用裸「異常」條款被認爲是不好的做法。改爲使用特定的例外。 –

0

您正在檢查list.append的返回值是否爲整數,而不是。因此,它將其視爲一個字符串。

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: ") 
    lis.append(x) 
    if(x=='stop'): 
     break 

for items in lis: 
    if items.isdigit(): # check whether is integer 
     num += 1 
    else: 
     lett += 1 

print(lis) 
print("there are " + str(num) + " numbers") 
print("there are " + str(lett) + " strings") 
0

這裏是我的解決方案,它在我的Python IDLE版本3.6.0工程(請回復,如果它不爲你工作):

lis = []   # list name 

num, lett = 0, 0 # init counters of numbers and letters 

while True: 
    try: 
     x = input("digit an int or string, 'stop' returns values: ") 
     i = lis.append(x) 
     if(x=='stop'): 
      False 
      break 
    except ValueError: 
     print("Not an integer!") 
     continue 
    else: 
     num += 1 

print(lis) 
print("there are " + str(num) + " numbers") 
print("there are " + str(lett) + " strings") 
+0

目前它不識別字符串,每個值都被視爲整數,因爲它只會增加_num_,即使我寫'蛋' –

0

這裏是我的代碼

while True: 
    l,num,lett = [],0,0 
    while True: 
     x = input('digit an int or string, "stop" returns values: ').lower().strip() 
     try: 
      x = int(x) 
     except: 
      pass 
     if x == ('stop'): 
      break 
     l.append(x) 

    for element in l: 
     if isinstance(element, int): 
      num += 1 
     else: 
      lett += 1 

    print (l) 
    print ("there are " + str(num) + " numbers") 
    print ("there are " + str(lett) + " strings") 
    l,num,lett = [],0,0  #reset to go again 
0

您可以使用isdigit和你的代碼改成這樣:

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 
    if x.isdigit(): # check whether is integer 
     lis.append(int(x)) 
     num += 1 
    else: 
     lis.append(x) 
     lett += 1 

print(lis) 
print("there are " + str(num) + " numbers") 
print("there are " + str(lett) + " strings") 

而且,你可以用這個解決您的代碼(它需要一個縮進,並將其移動到主while):

if isinstance(int(x), int): # check whether is integer 
    num += 1 
    lis.append(int(x)) 
else: 
    lett += 1 
    lis.append(x) 
+0

好吧,它確實有用,謝謝。還有一個問題:當我打印列表時,我看到這個輸出['value1','value2','value3']。 這意味着列表中充滿了字符串,不是嗎? 如果value1是一個整數,輸出是[value1,'value2','value3'],還是不是? –

+0

@JakCooper是的!我更新了我的答案;) – RaminNietzsche