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.
-
4Please edit your code to fix the indentation. It's not clear what code is supposed to be where.BrenBarn– BrenBarn2012年07月25日 22:27:19 +00:00Commented Jul 25, 2012 at 22:27
2 Answers 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
answered Jul 25, 2012 at 22:32
jmetz
13k3 gold badges33 silver badges41 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
hexparrot
The
cash < 0 should probably be cash <= 0, as you would be out of money there, too, right?Eddie Tallberg
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.
jmetz
@EddieTallberg - I think my listing solves both of these problems.
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
jmetz
This will make
Lottery recursively call itself and never end.. I have a feeling the OP made a mistake designing the program like this.Ry-
@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.jmetz
Ahh right missed the
break - so... finite recursion. Lottery calling itself is not a good idea...see my answer above.Ry-
@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.
jmetz
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.
|
lang-py