计算范围内的数字

时间:2013-12-12 18:09:47

标签: python

我必须编写一个Python脚本,允许用户输入一组正数,然后计算有多少

  • 等于50
  • 大于50
  • 小于50

使用适当的输出来证明您的程序正在运行。

2 个答案:

答案 0 :(得分:3)

仅举例来说,让我们编写一个函数来计算有多少数字超过50,然后一个等于50,然后一个少于50.免责声明:这不是唯一的,甚至是最好的方法你想做什么,这只是最好的教具:)。此外,如果您不使用Python 3.x,某些命名法可能会改变。

#this function returns how many values in the list that's passed to it
#are greater than 50
def greaterThanFifty(list_to_compare):
    how_many_above_fifty = 0
    for value in list_to_compare:
        if value > 50:
            how_many_above_fifty += 1
    return how_many_above_fifty

#this function returns how many values in the list that's passed to it
#are less than 50
def lessThanFifty(list_to_compare):
    how_many_under_fifty = 0
    for value in list_to_compare:
        if value < 50:
            how_many_under_fifty += 1
    return how_many_under_fifty

#this function returns how many values in the list that's passed to it
#are equal to 50
def equalToFifty(list_to_compare):
    how_many_are_fifty = 0
    for value in list_to_compare:
        if value == 50:
            how_many_are_fifty += 1
    return how_many_are_fifty

现在我们的函数将返回我们需要的值。同样,这不是唯一或甚至是最好的方法,但这是我使用的方式,因为它教授了许多基本策略,如编写模块化程序(每个函数由其自己的代码执行)和使用功能而不是直接代码来执行您的任务。不幸的是,这些代码本身并没有做任何事情。它只是定义了我们将利用的功能来解决手头的问题。从本质上讲 - 我们制作了锤子,钉子和锯子,但现在我们必须将木材切割成大小并将其钉起来。我们这样做。

def main(): #this is always the name of our function that does the heavy lifting
    list_of_numbers = []
    user_input = input("List some numbers, comma separated please: ")
    for num in user_input.split(","):
    #loop through user_input, split by commas
        list_of_numbers.append(num.strip())
        #add to list_of_numbers each item that we find in user_input,
        #stripped of leading and trailing whitespace
    #at this point we've looped through all of user_input and added each number
    #to list_of_numbers, so we can use our functions, defined earlier, to return
    #the requested values. Luckily we set up our functions to accept
    #lists!

    greater_than_fifty = greaterThanFifty(list_of_numbers)
    less_than_fifty = lessThanFifty(list_of_numbers)
    equal_to_fifty = equalToFifty(list_of_numbers)

    #now just to display the results to the user, and we're done.
    print("There are "+str(greater_than_fifty)+" numbers over fifty")
    print("There are "+str(less_than_fifty)"+ numbers under fifty")
    print("There are "+str(equal_to_fifty)"+ numbers that are fifty")

我们仍然没有实际完成任何事情,因为我们所做的就是定义从头到尾完成我们想要的功能。我们的greaterThanFifty函数是我们的锤子,我们的lessThanFifty函数是我们的锯,我们的equalToFifty函数是我们的指甲。现在我们添加了一个main函数,这是我们的勤杂工。现在我们需要告诉他工作。幸运的是,这很容易:)

main()

就是这样!我们完成了!

为了进行比较,我就是这样写的:

input_list = [int(each.strip()) for each in input("Enter a list of numbers, comma-separated: ").split(",")]
print("{} are less than 50, {} are more than 50, {} are 50".format(len([each for each in input_list if each<50]),len([each for each in input_list if each>50]),len([each for each in input_list if each==50])))

你会到达那里,年轻的padawan:)

答案 1 :(得分:0)

def main():
    inp = input('enter number of positive numbers to enter ')
    print 'enter the list of positive numbers'
    num = []
    count_eq = 0
    count_gr = 0
    count_ls = 0
    for elem in range(inp):
        num.append(input(''))
    for item in num:
        if item == 50:
            count_eq += 1
        if item > 50:
            count_gr += 1
        if item < 50:
            count_ls += 1
    print '%d are equal %d are less than and %d are greater than 50' % (count_eq, count_ls, count_gr)
main()

这是一个非常基本的程序!你应该开始python的初学者教程here

相关问题