Sulumar
Sulumar

Reputation: 87

recursive number generator not working

I'm currently learning python, and while trying to write a simple lotto program, I came across a little problem.

I'm trying to do is generate two lists, each containing six numbers. the first one is generatedby asking the user for a number.

The second one generates random integers between 1 and 49 by using randint

If I code it like this:

from random import randint

lottoschein = []
gewinnzahlen = []

while (len(lottoschein)<6):
  lottoschein.append(int(input("Bitte geben sie eine Zahl ein: ")))# Eingabeaufforderung
  print (lottoschein)
  print (len(lottoschein))


while (len(gewinnzahlen)<6):
   zahl = randint (1,49)
   gewinnzahlen.append(zahl)
   print (gewinnzahlen)

It actually works and generates a nice output like

Bitte geben sie eine Zahl ein: 1
[1]
1
Bitte geben sie eine Zahl ein: 5
[1, 5]
2
Bitte geben sie eine Zahl ein: 4
[1, 5, 4]
3
Bitte geben sie eine Zahl ein: 4
[1, 5, 4, 4]
4
Bitte geben sie eine Zahl ein: 5
[1, 5, 4, 4, 5]
5
Bitte geben sie eine Zahl ein: 8
[1, 5, 4, 4, 5, 8]
6
[40]
[40, 33]
[40, 33, 42]
[40, 33, 42, 20]
[40, 33, 42, 20, 35]
[40, 33, 42, 20, 35, 14]

Just what I asked it to do.
But if I try coding it in a recursive function like this:

from random import randint

lottoschein = []
gewinnzahlen = []

def zahlen (schein,zahl):
  if(len(schein)<6):
   schein.append(zahl)
   return zahlen (schein,zahl)
  else:
    print (schein)
    return schein

lottoschein = zahlen (lottoschein, int(input("Bitte geben sie eine Zahl ein: ")))
gewinnzahlen = zahlen (gewinnzahlen, randint (1,49))

it returns an output like:

Bitte geben sie eine Zahl ein: 2
[2, 2, 2, 2, 2, 2]
[17, 17, 17, 17, 17, 17]

seems like it generates the numbers only once.
What I don't understand is why. Could anyone help me get the recursive function working please?

Upvotes: 0

Views: 266

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1123590

Once you call randint(), it returns a number. It is that number that is passed into the function, not the function.

In other words, you only call the function once and reuse the output, over and over again.

Your while loops, on the other hand, re-execute the loop body each iteration, so in that case the input() function or randint() function is called for each iteration of the while loop and thus input a new number.

If you wanted a function called each time you go through the loop, pass in the function and arguments instead:

def zahlen(schein, function, *arguments):
    if len(schein) < 6:
        schein.append(function(*arguments))
        return zahlen(schein, function, *arguments)
    return schein

and call this with:

lottoschein = zahlen(lottoschein, lambda: int(input("Bitte geben sie eine Zahl ein: "))))
gewinnzahlen = zahlen(gewinnzahlen, randint, 1, 49)

Now either lambda: int(input('....')) (with no arguments) is called each time in the recursive function, or randint is called with the arguments 1 and 49.

Demo:

>>> def zahlen(schein, function, *arguments):
...     if len(schein) < 6:
...         schein.append(function(*arguments))
...         return zahlen(schein, function, *arguments)
...     return schein
... 
>>> from random import randint
>>> zahlen([], randint, 1, 49)
[42, 11, 41, 28, 27, 24]
>>> zahlen([], lambda: int(input('Please enter a number: ')))
Please enter a number: 10
Please enter a number: 20
Please enter a number: 30
Please enter a number: 42
Please enter a number: 38
Please enter a number: 2
[10, 20, 30, 42, 38, 2]

Upvotes: 1

Steve
Steve

Reputation: 385

Your problem is in the line

   return zahlen (schein,zahl)

Here, you are calling zahlen with the same integer zahl that was used on the first call. Try to replace it with:

return zahlen(schein, int(input("Bitte geben sie eine Zahl ein: ")))

Upvotes: 0

Related Questions