0

So I want to make a simple loop program but I have a problem:

def Lottery():
 Cash = 200
 YourNumber = randint(1, 10)
 while YourNumber != WinningNumber:
 Cash = Cash - 10
 if Cash < 0:
 print("You are out of money!")
 break
 YourNumber = randint(1, 10)
 else:
 Cash = Cash + 100
 Lottery()

The problem is that in the last line of the def the "cash" will automatically reset to 200 again when restarting the loop. Maybe there is a really simple solution for this, but I've googled and tried without any results.

BrtH
2,70319 silver badges29 bronze badges
asked Jul 25, 2012 at 22:25
1
  • 4
    Please edit your code to fix the indentation. It's not clear what code is supposed to be where. Commented Jul 25, 2012 at 22:27

2 Answers 2

2

The same thing (infinite loop but breaks if you run out of money, without the recursive calling),

def Lottery():
 Cash = 200
 YourNumber = randint(1,10)
 while 1:
 if YourNumber != WinningNumber:
 Cash = Cash - 10
 if Cash <= 0:
 print("You are out of money!")
 break
 YourNumber = randint(1,10)
 else:
 Cash = Cash + 100
Ry-
226k56 gold badges496 silver badges504 bronze badges
answered Jul 25, 2012 at 22:32
Sign up to request clarification or add additional context in comments.

3 Comments

The cash < 0 should probably be cash <= 0, as you would be out of money there, too, right?
Well the problem is that I wanted to have 200 to start off with, so you had some more chances to win. I don't know how to both get the 200 to start off with and then not reset it every time you win.
@EddieTallberg - I think my listing solves both of these problems.
1

Pass Cash as an argument, setting a default value:

def Lottery(Cash = 200):
 YourNumber = randint(1,10)
 while YourNumber != WinningNumber:
 Cash = Cash - 10
 if Cash < 0:
 print("You are out of money!")
 break
 YourNumber = randint(1,10)
 else:
 Cash = Cash + 100
 Lottery(Cash)

Code tip: you can use += and -= as shortcuts for addition/subtraction and assignment, plus a couple other changes:

def lottery(cash = 200):
 while randint(1, 10) != WinningNumber:
 cash -= 10
 if cash <= 0:
 print("You are out of money!")
 break
 else:
 lottery(cash + 100)
answered Jul 25, 2012 at 22:28

7 Comments

This will make Lottery recursively call itself and never end.. I have a feeling the OP made a mistake designing the program like this.
@mutzmatron: How so? It'll start out with 200 in Cash, then each time either decreasing or reaching the else part, where it restarts with more Cash. If Cash reaches zero, all of the functions will end. No infinite recursion.
Ahh right missed the break - so... finite recursion. Lottery calling itself is not a good idea...see my answer above.
@mutzmatron: It could be homework that requires the use of recursion. Having a program that just generates random numbers and always ends in "You are out of money!" is not really a good idea either.
You could be right - though I only saw simple loop in the OPs post, so I went down the simpler, recursion free path. But again, you could have interpreted correctly, who knows.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.