2013-03-11 52 views
-1

分組串輸入我正在試圖做的一個問題了我的書,並問:通過計數

實現功能名稱,它沒有任何輸入,反覆詢問 用戶輸入一個學生的名字。當用戶輸入一個空白 字符串時,該功能應打印每個名稱,即具有該名稱的 學生的編號。

用法示例:

Usage: 
names() 
Enter next name: Valerie 
Enter next name: Bob 
Enter next name: Valerie 
Enter next name: John 
Enter next name: Amelia 
Enter next name: Bob 
Enter next name: 
There is 1 student named Amelia 
There are 2 students named Bob 
There is 1 student named John 
There are 2 students named Valerie 

到目前爲止,我有這樣的代碼:

def names(): 
    names = [] 
    namecount = {a:name.count(a) for a in names} 
    while input != (''): 
     name = input('Enter next name: ') 
     names = name 
     if input == ('') 
      for x in names.split(): 
       print ('There is', x ,'named', names[x]) 

我真的在這裏失去了任何的投入將助陣噸。另外,如果可能的話,請解釋如何修復我的代碼

+0

@NPE我想他的意思來解釋我們的答案 – TerryA 2013-03-11 07:54:47

+0

@Haidro是的,這就是我的意思,抱歉的混亂。我只是真的迷失在這裏,而我一直在這個問題上掙扎了一段時間。 – JGrazza 2013-03-11 07:56:08

回答

0

在你的函數中namings有很多問題,你正在使用'names'這樣的變量,用於函數名以及'input'是一個用於讀取用戶輸入的python函數名 - 所以你必須避免使用這個。你也定義一個namecount變量作爲一個字典,並嘗試在填充前初始化它。因此,嘗試檢查下面的解決方案:

def myFunc(): 
    names = [] 
    name = '' 
    while True: #bad stuff you can think on your own condition 
     name = raw_input('press space(or Q) to exit or enter next name: ') 
     if name.strip() in ('', 'q', 'Q'): 
      for x in set(names): 
       print '{0} is mentioned {1} times'.format(x, names.count(x)) 
      break 
     else: 
      names.append(name) 

myFunc() 

OR:

from collections import defaultdict 

def myFunc(): 
    names = defaultdict(int) 
    name = '' 
    while True: #bad stuff you can think on your own condition 
     name = raw_input('press space(or Q) to exit or enter next name: ') 
     if name.strip() in ('', 'q', 'Q'): 
      for x in set(names): 
       print '{0} is mentioned {1} times'.format(x, names[x]) 
      break 
     else: 
      names[name] += 1 
0

我改寫了你的功能爲您提供:

def names(): 
    names = {} # Creates an empty dictionary called names 
    name = 'cabbage' # Creates a variable, name, so when we do our while loop, 
        # it won't immediately break 
        # It can be anything really. I just like to use cabbage 
    while name != '': # While name is not an empty string 
     name = input('Enter a name! ') # We get an input 
     if name in names: # Checks to see if the name is already in the dictionary 
      names[name] += 1 # Adds one to the value 
     else: # Otherwise 
      names[name] = 1 # We add a new key/value to the dictionary 
    del names[''] # Deleted the key '' from the dictionary 
    for i in names: # For every key in the dictionary 
     if names[i] > 1: # Checks to see if the value is greater for 1. Just for the grammar :D 
      print("There are", names[i], "students named", i) # Prints your expected output 
     else: # This runs if the value is 1 
      print("There is", names[i], "student named", i) # Prints your expected output 

在做names()

Enter a name! bob 
Enter a name! bill 
Enter a name! ben 
Enter a name! bob 
Enter a name! bill 
Enter a name! bob 
Enter a name! 
There are 3 students named bob 
There are 2 students named bill 
There is 1 student named ben 
+0

感謝您的反饋!你的意見真的幫助我看到代碼如何工作 – JGrazza 2013-03-11 08:34:11

+0

@JGrazza沒有問題:)。如果解決方案是有益的,隨時接受:) – TerryA 2013-03-11 08:40:07

0

讓我們來分析你的代碼:

def names(): 
    names = [] 
    namecount = {a:name.count(a) for a in names} 
    while input != (''): 
     name = input('Enter next name: ') 
     names = name 
     if input == ('') 
      for x in names.split(): 
       print ('There is', x ,'named', names[x]) 

似乎有是幾個問題,讓我們來一一列舉了

  1. while循環的條件
    • 你想要做的檢查,如果從用戶輸入''(什麼)什麼..
    • input是獲取用戶輸入的內置函數,所以它永遠不會是('')
  2. names = name聲明
    • 你想要做什麼是添加name到列表names
    • 這裏你將names更改爲一個字符串,這不是你想要的。
  3. if的條件
    • 相同1。
  4. for
    • 讓我們忽略..只是無效..在這裏..

我們解決這些問題如下(解決方案具有相同的編號以上問題,它解決)

  1. 將條件更改爲像name != ''。 另外,在循環開始之前,您需要輸入一次才能正常工作,在這種情況下有獎金,第一個輸入可以有不同的提示。
  2. 使用names.append(name)添加namenames
  3. 同1
  4. 只要看看下面循環...

試試這個

def names(): 
    names = [] 
    name = input('Enter a name: ').strip() # get first name 
    while name != '': 
     names.append(name) 
     name = raw_input('Enter next name: ').strip() # get next name 

    for n in set(names): # in a set, no values are repeated 
     print '%s is mentioned %s times' % (n, names.count(n)) # print output 
+0

你好。你從法語翻譯成英文我回答這個問題:(http://stackoverflow.com/questions/15336747/python-and-string-extraction#comment21659597_15336747)不是一個大問題。但你完全改變了內容,因此意義。我並沒有喚起SQL,而是你做的,加上_「處理各種基礎和文本可能很複雜。」_暗示我用正則表達式的答案不適用。這是你的意見,不是我的。我沒有感激。所以我刪除了「我的」答案。 – eyquem 2013-03-11 11:45:43

+0

@eyquem這就是[谷歌](http://translate.google.co.in/#auto/en/Bonjour%0A%0Afait%20ce%20qu%27il%20faut.%20Ce%20peut%20%C3%AAtre %20complexifi%C3%A9%20pour%20pouvoir%20traiter%20une%20vari%C3%A9T%C3%A9%20de%20bases%20et%20et%20de%20texte)說......所以,認爲這是谷歌的意見:)。對不起......我剛剛添加了關於MySQL庫的部分...... – pradyunsg 2013-03-11 11:50:11

+0

Google的翻譯是「做正確的事情,處理各種數據庫和文本以及」_(順便說一下,應該是**做的是正確的事情**,谷歌的翻譯是不好的),但修改後的答案中的內容是「使用它所需要的(一個合適的sql庫)。處理各種基礎和文本可能很複雜。「_你會假裝它是一樣的嗎?我沒有寫短語**一個合適的SQL庫**,我沒有寫**使用它需要的**。 – eyquem 2013-03-11 12:08:54

0
def names(): 
     counters = {} 

     while True: 
      name = input('Enter next name:') 

      if name == ' ': 
        break 

      if name in counters: 
        counters[name] += 1 
      else: 
        counters[name] = 1 
     for name in counters: 
      if counters[name] == 1: 
        print('There is {} student named {}'.format(counters[name],name)) 
      else: 
        print('There are {} student named {}'.format(counters[name],name)) 


names()