简体   繁体   English

如何将列表和用户号码传递给函数并让它显示列表中大于用户号码的所有数字?

[英]How do I pass both the list and the user's number to a function and have it display all numbers in the list that are greater than the user's number?

I need to pass both the list and the user's number to a function and have it display all numbers in the list that are greater than the user's number.我需要将列表和用户号码都传递给一个函数,并让它显示列表中大于用户号码的所有数字。 This is as far as I've gotten and am stuck.这是我已经得到的并且被卡住了。 Thanks for any tips.感谢您提供任何提示。

import random

def randnum():
    random_num = [random.randrange(1,101,1) for _ in range (20)]
    random_num.sort()
    return random_num

def usernum():
    try:
        user_num = int(input("Please enter a number 1 through 100: "))
        if user_num > 100 or user_num < 1:
            print("Please try again.")
            usernum()
    except ValueError:
        user_num = print("Error. Please try to use integers while entering a number 1-100")
        usernum()
    return user_num

def main():

I think you are missing some code, but your function could be something like that :我认为您缺少一些代码,但您的功能可能是这样的:

def getHigherNumbers(userNumber, listNumbers):
    return [x for x in listNumbers if x > userNumber]

Also there is a bug in your usernum() :您的usernum()还有一个错误:

When there is an error you should use return usernum() instead of just usernum() since there'll be recursion.当出现错误时,您应该使用return usernum()而不是usernum()因为会有递归。

To answer your question in the comments, here is how your code could look :要在评论中回答您的问题,以下是您的代码的外观:

import random

def randnum():
    random_num = [random.randrange(1,101,1) for _ in range (20)]
    random_num.sort()
    return random_num

def usernum():
    try:
        user_num = int(input("Please enter a number 1 through 100: "))
        if user_num > 100 or user_num < 1:
            print("Please try again.")
            return usernum()
    except ValueError:
        print("Error. Please try to use integers while entering a number 1-100")
        return usernum()
    return user_num

def getHigherNumbers(user_num, random_num):
    return [x for x in random_num if x > user_num]

def main():
    random_num = randnum()
    print('random nums : %s' % random_num)
    user_num = usernum()
    print('user num : %s' % user_num)
    greater_nums = getHigherNumbers(user_num, random_num)
    print('greeter numbers : %s' % greater_nums)

if __name__ == '__main__':
    main()


# random nums : [1, 4, 11, 14, 18, 24, 27, 29, 31, 37, 37, 41, 45, 59, 59, 66, 83, 87, 90, 99]
# Please enter a number 1 through 100: 12
# user num : 12
# greeter numbers : [14, 18, 24, 27, 29, 31, 37, 37, 41, 45, 59, 59, 66, 83, 87, 90, 99]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - 对于每个数字,都有一个比它大一级的所有数字的列表 - Python - For each number, have a list of all numbers that are one level greater 如何在 python 中删除列表中大于或等于指定数字的数字 - How do you remove numbers greater than or equal to a specified number within a list, in python 仅当较早的数字小于当前数字时,如何在 python 列表中找到数字的差异 - how to find the difference of a number and it's earlier numbers in python list only if the earlier number is less than the current number 如何在列表中找到比我的号码大的最接近的号码? - How do I find the closest number in a list that is greater than my number? 在列表中找到连续数字大于“ n”的最后一个数字 - find the last number in list with consecutive number of numbers greater than “n” 我想在定义 function 中传递一个列表来计算列表中数字的位数 - I want to pass a list in a definition function to count the number of digits in a number that's in a list 询问用户5个数字,并显示一条消息,说明该数字是否在列表中 - Ask the user for 5 numbers and display a message saying whether the number was on the list or not 如何计算列表中的数字大于其后的数字的次数? - How to count the number of times a number in a list is greater than the number after it? 您如何让用户输入一个数字并让该数字从列表中提取该数据单元格值? - How do you have a user input a number and have that number pull that data cell value from a list? 我的部分代码应该检查用户 i 输入的数字是否在 1 到 8 之间,但它接受的数字是否大于 8 - Part of my code is supposed to check if the number entered by user i between 1 to 8 but it's accepting numbers > than 8
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM