2012-12-18 56 views
1

所以我有一個關於Python中的字典的問題。 我想創建一個字典,用戶將被提示2個選項;更新字典或清除字典。 讓我先告訴你我的代碼:在Python中更新和清除字典

def myStuff(): 
    food = {'Apple': 0, 'Banana': 0, 'Grapes': 0} 
    choice = raw_input('Please pick an option:\n1) Update number of food I have\n2) Clear all\n>>') 
    if choice == str(1): 
     apple = int(raw_input('How many apples do you want to add?\n>>')) 
     banana = int(raw_input('How many bananas do you want to add?\n>>')) 
     grapes = int(raw_input('How many grapes do you want to add?\n>>')) 
     print 'Updating...' 
     food['Apple'] = apple 
     food['Banana'] = banana 
     food['Grapes'] = grapes 
     print food 
    elif choice == str(2): 
     food['Apple'] = 0 
     food['Banana'] = 0 
     food['Grapes'] = 0 
     print food 
    else: 
     return False 

myStuff() 

現在,這裏的想什麼,我補充:

1.能力,爲用戶不斷更新的字典(這意味着如果有人輸入10蘋果,詞典將存儲10個蘋果,並且將再次提示用戶輸入他想要輸入的蘋果的數量來更新詞典)。 我真的不知道如何實現一個循環。

2,用戶更新後清除字典的能力。例如:如果有人輸入10個蘋果,循環會再次詢問用戶他/她是否想清除字典。

這有點像銀行,有人存款和清除賬戶,如果他們沒有任何錢留在那裏帳戶。

回答

0

你在做什麼,它可能是更好的創造自己的類。對於您希望執行的所有不同操作,您也應該具有不同的功能。即。更新,清除,添加

class Fruit: 
    Apple = 0 
    Banana = 0 
    Grape = 0 

    def __repr__(self): 
     return "You have {self.Apple} apples, {self.Banana} bananas, and {self.Grape} grapes".format(**locals()) 

    def update(self): 
     while 1: 
     choice = raw_input('Please pick an option:\n1) Update number of food I have\n2) Clear all\n>>') 
     if (int(choice) == 1): 
      self.Add() 
     elif (int(choice) == 2): 
      self.Clear() 
     else: 
      print "Input not valid" 

    def Add(self): 
     self.Apple += int(raw_input('How many apples do you want to add?\n>>')) 
     self.Banana += int(raw_input('How many bananas do you want to add?\n>>')) 
     self.Grape += int(raw_input('How many grapes do you want to add?\n>>')) 
     print self 

    def Clear(self): 
     self.Apple = 0 
     self.Banana = 0 
     self.Grape = 0 
     print self 

if __name__ == "__main__": 
    fruit = Fruit() 
    fruit.update()   

你也應該在看使用tryexcept語句,以確保程序不會崩潰,如果使用了錯誤的輸入。你也應該添加一個退出命令來退出程序,否則這將永遠循環。例如。如果用戶輸入是「退出」,則有條件通知它並且break

0
  1. 對於在循環更新,也許你可以試試:

    for key in food: 
        food[key] += int(raw_input('How many %s do you want to add?> ' % key) or 0) 
    
  2. 您已經由值設置成零清空字典。

+0

感謝您的幫助球員。 我覺得字典應放在函數之前。 當rkd91回答時,我用if語句實現了一個while循環。一切現在完美。 –

0

如果我理解正確 - 你想不斷問這個問題嗎?在這種情況下,你需要做的就是把while True:並縮進你想要重複的代碼塊,並且它將永遠循環。您可能希望從raw_input(Python 3中的input)到elif之後 - 或者,如果您使food爲全局變量(因此每次調用myStuff()時都不會重新初始化爲0) ,你可以這樣做:

while True: 
    myStuff() 
+0

附錄:「非法」輸入的返回值爲「False」。我將使用該返回值作爲while循環的條件。 – Moshe

0

你可以做到這一點是這樣的:

""" 
in each loop check the fruit number to see if it is 0 or not.if it 
is 0, then this means that you don't want to add fruit from the current 
type and skips to the nextFruit.Focus on the code and you realize the other parts. 
"""  

import string 

def myStuff(): 
    food = {'Apples': 0, 'Bananas': 0, 'Grapes': 0} 
    choice = raw_input('Please pick an option:\n1) Update number of food I have\n2) Clear all\n>>') 
    fruit = 0 
    nextFruit = 'Apples' 
    while True: 
     if choice == str(1): 
      fruit = int(raw_input('How many ' + str.lower(nextFruit) + ' do you want to add?\n>>')) 
      if fruit == 0 and nextFruit == 'Apples': 
       nextFruit = 'Bananas' 
       continue 
      elif fruit == 0 and nextFruit == 'Bananas': 
       nextFruit = 'Grapes' 
       continue 
      elif fruit == 0 and nextFruit == 'Grapes': 
       print 'Updating...' 
       print food 
       choice = raw_input('Please pick an option:\n1) Update number of food I have\n2) Clear all\n>>') 
      else: 
       food[nextFruit] += fruit 
     elif choice == str(2): 
      food['Apples'] = 0 
      food['Bananas'] = 0 
      food['Grapes'] = 0 
      print 'Updating...' 
      print food 
      choice = raw_input('Please pick an option:\n1) Update number of food I have\n2) Clear all\n>>') 
     else: 
      print "You've entered a wrong number....try again please:" 
      print 
      choice = raw_input('Please pick an option:\n1) Update number of food I have\n2) Clear all\n>>')