[英]I would like to know if this is possible in python
I was wondering if it is possible to display "Enter the number of wins on monday, tuesday, wednesday, and so on" sequentialy after each input.我想知道是否可以在每次输入后按顺序显示“输入星期一、星期二、星期三等的获胜次数”。 The only method I can think of is to make multiple inputs in the module.
我能想到的唯一方法是在模块中进行多个输入。
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
You could do something like:你可以这样做:
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
"""
The only method I can think of is to make multiple inputs in the module
我能想到的唯一方法是在模块中进行多个输入
raw_input
returns a stringraw_input
返回一个字符串You could let the user enter the values for the whole week in one raw_input
, separated by spaces, comma, etc.您可以让用户在一个
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)
You can use map
to create a list of all the inputs made by user as:您可以使用
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)
Now you can simply do:现在你可以简单地做:
totalwins = sum(dailywins)
to get totalwins
获得
totalwins
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.