2016-03-08 57 views
-1

我想知道是否可以在每次輸入後按順序顯示「在週一,週二,週三等等輸入贏數」。我能想到的唯一方法是在模塊中輸入多個輸入。我想知道這是否可能在python

def getWins(): 
counter = 1 
totalWins = 0 
dailyWins = 0 
while counter <= 7: 
    dailyWins = raw_input('Enter the number of wins acquired for each day this week:') 
    totalWins = totalWins + dailyWins 
    counter = counter + 1 
return totalWins 
+0

您也可以將輸入作爲列表並相應地解析它。 你到底在問什麼? –

+0

你的問題不清楚。請詳細說明。 –

+0

感謝您的回覆,對於未詳細說明的問題,我一直在尋找的是程序在每次用戶輸入後列出一週中的不同日期,以便用戶可以輸入一週中不同日期的數據 –

回答

1

你可以這樣做:

def getWins(): 
    week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] 
    scores = {} # Perhaps you want to return a dictionary? If not, just set this to 0 and += score below. Also, remove sum() 
    for day in week: 
     score = int(raw_input("Enter the number of wins on {}: ".format(day))) 
     scores[day] = score 
    return sum(scores.values()) 

getWins() 
""" 
>>> getWins() 
Enter the number of wins on Monday: 5 
Enter the number of wins on Tuesday: 4 
Enter the number of wins on Wednesday: 5 
Enter the number of wins on Thursday: 1 
Enter the number of wins on Friday: 3 
Enter the number of wins on Saturday: 7 
Enter the number of wins on Sunday: 9 
34 
""" 
+0

感謝好東西,這正是我正在尋找的東西,我正在嘗試爲我的介紹編程類創建一個數據跟蹤程序 –

0

我可以想到的唯一方法是在模塊中,以使多個輸入

  • 在Python 2.7,raw_input返回字符串
  • 字符串可以被劃分

您可以讓用戶輸入整個星期的值在一個raw_input,用空格,逗號分隔等

def get_wins(): 
    data = raw_input('Enter the number of wins for each day, separated by spaces: ') 

    wins = [int(win) for win in data.split() if win.isdigit()] 
    return sum(wins) 
+0

是的,但可以如果用戶將所有值都放入1個輸入中,我會在最後得到一個總值? –

0

您可以使用map創建的用戶作爲所做的所有輸入列表:

dailywins = map(int, raw_input("enter the daily wins of whole week").split()) 
# THIS gives you a list object of ints which can be manipulated any way you want(here input is space separated) 

現在,你可以簡單地做:

totalwins = sum(dailywins) 

得到totalwins