I have this short code here. But it is not printing in wing ide 4.1 using python because it is an infinite loop. Any ideas of what I could add or how to fix it so it prints?
import random
coins = 1000
wager = 2000
while ((coins>0) and (wager!= 0)):
x = random.randint(0,10)
y = random.randint(0,10)
z = random.randint(0,10)
print x,
print y,
print z
The code you posted won't do anything but pick 3 psuedo-random numbers until the end of time. You have to add some win/loss conditions. As of now the x, y and z are just numbers. If you want to make a gambling game you have to add some victory conditions like:
if x + y + z > 10
just an example but your program needs to be able to tell if the player won. then it needs to make changes to the players total amount of money and ask for a new wager. You also might want to add logic to make sure the player can't bet more than they have.
import random
coins = 1000
wager = 0
while True: #main loop
print('you have {} coins'.format(coins))
if coins == 0: #stops the game if the player is out of money
print('You are out of money! Scram, deadbeat!')
break
while wager > coins or wager == 0: #loops until player enters a non-zero wager that is less then the total amount of coins
wager = int(input('Please enter your bet (enter -1 to exit): '))
if wager < 0: # exits the game if the player enters a negative
break
print('All bets are in!')
x = random.randint(0,10)
y = random.randint(0,10)
z = random.randint(0,10)
print(x,y,z) #displays all the random ints
if x + y +z > 10: #victory condition, adds coins for win
print('You win! You won {} coins.'.format(wager))
coins += wager
else: #loss and deduct coins
print('You lost! You lose {} coins'.format(wager))
coins -= wager
wager = 0 # sets wager back to 0 so our while loop for the wager validation will work
Your code never alters coins
or wager
, so the while
condition is always True:
while ((coins>0) and (wager!= 0)):
# code that doesn't touch coins or wager
Perhaps you meant to also subtract one of x
, y
or z
from coins
or wager
?
As to why the code doesn't print in Wing IDE; this depends entirely on your indentation. If the print
statements are not part of the loop, they are never reached and never executed. Try creating a loop that does not run infinitely.
Because there is no modification to your breaking condition in your while loop.
In your case, the breaking condition is when wager is not 0
and coins > 0
, so you have to modify either the coins
or wager
variable in your code, e.g.
import random
coins = 1000; wager = 2000
while coins > 0 and wager is not 0:
x,y,z = [random.randint(0,10)]*3
print x,y,z
wager-= 1000
coins-= 100
As @martijn Indentation is important in python, see http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Indentation:
for _ in range(10):
x = 'hello world'
print x
[out]:
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
When the print x
is not indented:
for _ in range(10):
x = 'hello world'
print x
[out]:
hello world