简体   繁体   English

我想知道这在python中是否可行

[英]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我能想到的唯一方法是在模块中进行多个输入

  • In Python 2.7, raw_input returns a string在 Python 2.7 中, raw_input返回一个字符串
  • Strings can be split字符串可以拆分

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.

相关问题 我想知道如何在python中将ISO日期更改为UTC - I would like to know how to change an ISO date to UTC in python 您好,我想知道如何在python中使用Enum - Hello, I would like to know how to use Enum in python Python我想知道如何使用for循环编写(DictWriter) - Python i would like to know how to writerow with a for loop (DictWriter) 如何将此列表转换为数据框? 我想知道如何在python中做到这一点? - How to turn this list into a data frame? I would like to know how to do this in python? 使用 python 我想知道一个点(具有三个坐标)是否更接近给定的其他点的 2 厘米 - Using python I would like to know if one point (with three coordinates) is closer to 2 cm of other point given 我想知道如何使用 Bot 框架 SDK 为 python 设置自适应卡的主机配置 - I would like to know how to set the host config of the adaptive card using Bot framework SDK for python 我无法连接到 mqtt 中的代理,我想知道为什么? - I cannot connect to a broker in mqtt and I would like to know why? 我想知道如何制作while循环? - I would like to know how can I make while loop? 是否可以在执行之前知道是否将使用ast Python更改变量? - Is it possible to know before execution if the variable would be changed with ast Python? 我想知道如何在 Pytorch 中处理 LSTM - I would like to know how to handle LSTM in Pytorch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM