简体   繁体   中英

How could this function be implemented without recursion?

I'm trying to do a bingo game, I had some struggle with it but finally sorted it out. However, my main "problem" (more like, I've heard its bad programming) is that with my function I'm calling my function inside it in an else statement. I don't think that it's how you suppose to do it, i have not found any way around it though.. Because this function is called from another function called menu() so when i use a loop, it goes back to the menu if false.

Here's my code:

def selectingNumbers():
    numbers = []
    dupl = []
    j = 0
    print("Now you are gonna select 5 number\n")
    while j < 5:
        nummer = int(input("Your choice:\n"))
        numbers.append(int(nummer))
        j = j+1
    for i in numbers:
        if i not in dupl:
            dupl.append(i) #New list without duplicates
    if dupl == numbers: #Comparing new list with old list
        print("No equal numbers found")
        dragning(numbers)
    else:
        print("Equal numbers found")
        selectingNumbers() #Is there a better way to do it?

I also had some issues with the list at the beginning, I know I can use the set() function but i want to keep the original list as it is and compare the new one with the old one, can I do that in a better way with "real" programming instead of import modules?

Hope you can answer or guide me on these two questions with alternatives and if so, say why my code is "bad" if it is.

Well you have to decide if you want to use recursion to solve the problem. This line is a recursive call:

 selectingNumbers() #Is there a better way to do it?

Which is fine, and does not equate to bad programming. However, the rest of your function does no cater to a recursive function. You reset your variables and have no true base case because of that. See google, or here for examples.

Recursion is confusing for beginners, so I would take an iterative only approach. Here is a bingo python example .

In addition, I'm not sure this line works:

if dupl == numbers:  #Comparing new list with old list

I am not too familiar with python , but in my experience, arrays are treated as objects, so in that line you would be asking python to compare two seperate objects with unique references in memory. So they will never be equal, even if the values inside of them are the same because they are both referenced seperately. I found this link to answer that concern.

Recursion isn't "bad". In fact it can sometimes greatly simplify a solution to a problem. However in the case of your code it isn't necessary. Fortunately, it can sometimes be replaced with a loop. In the case of your code it looks like it could just loop until it gets a list from the user that doesn't contain any duplicates. That means it could be rewritten as shown below (I also simplified a few other things):

def selectingNumbers():
    while True:
        print("Now you are gonna select 5 different numbers\n")
        numbers = []
        for _ in range(5):
            number = int(input("Your choice:\n"))
            numbers.append(number)

        unique = set(numbers)  # will remove any duplicates

        if len(unique) == len(numbers):  # no dups?
            print("No equal numbers found")
            break  #  <--- terminates loop
        else:
            print("Equal numbers found")
            # allow loop to continue

    dragning(numbers)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM