函数 printGreater() [英] Function printGreater()

查看:37
本文介绍了函数 printGreater()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个函数 printGreater(),它将数字列表和数值作为参数.它打印列表中大于该值的数字,所有数字都在一行上,它们之间有一个空格.如果第一个参数提供一个空列表,则该函数不会打印任何内容.

I am trying to implement a function printGreater() that takes a list of numbers and a numeric value as parameter. It prints the numbers in the list that are greater than the value, all on one line with a space between them. If an empty list is provided as the first parameter, the function doesn't print anything.

这是我目前所拥有的:

def printGreater(nums, value):

lstN = (int[nums],value)
if nums > value:
    print(nums, end=", ")

推荐答案

def printGreater(nums, value):
    #First create an empty list to hold onto all the numbers larger than value
    greater = [];

    #Loop overall the input values, saving all large ones
    for num in nums:            
        if num > value:
            #Convert to a string for printing
            greater.append(str(num))

    #Print them out with spaces in between
    print( ' '.join(greater) )

#Then test with this
printGreater([1, 2, 3, 4, 5, 6, 7], 3)

这篇关于函数 printGreater()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆