Creating a game with python


#1

Making my game with python.I'm having a hard time with "while" loop.

import random
print ('Welcome to the Lottery')
dollar = 100
print 'Your money: $' + str(dollar)
print 'Enter your bet:'
bet= raw_input('(ex.$100)')

remaining = dollar - int(bet)
if bet <= dollar:
print 'Your money: $' + str(remaining)
print 'Bet:$' + bet
elif bet > dollar:
print 'You don\'t have enough money'
raw_input('(ex.$100)')
while bet > dollar:
print 'You don\'t have enough money'
raw_input('(ex.$100)')
else:
print 'You don\'t have any bets.'

raw_input('Enter you number (1-999):')
raw_input('Play? (yes or no)')
for i in range(1):
(random.randint(1,999))
print(random.randint(1,999))


#2

The input is in need of assignment so it can referred later.

Ditto, this one...

and this value, too...


#3

its not complete yet. i still get so many errors


#4

Do you have an interactive console? If so, try entering your code in the command line, and see what the response is. It will help you big time to track errors in ideas as you go, rather than four hours later. Piece your logic together with immediate responses. When you can package some test lines in a function. build it, test it again, and then move to the next segment.

>>> from random import randint
>>> print ("Welcome to the Lottery")
Welcome to the Lottery
>>> dollar = 100
>>> print ("Your money: ${}".format(dollar))
Your money: $100	
>>> bet = int(input("Enter your bet: "))
Enter your bet: 101
>>> if bet > dollar:
	print ("You do not have enough to cover the bet.")

	
You do not have enough to cover the bet.
>>> while bet > dollar:
	bet = int(input("Enter your bet: "))
	if bet > dollar:
		print ("You do not have enough to cover the bet.")

		
Enter your bet: 101
You do not have enough to cover the bet.
Enter your bet: 100
>>> print ("You bet ${}.".format(bet))
You bet $100.
>>>

And we can go on... The point is to test your ideas and iron out the wrinkles directly, then translate to program code.


#5

what is "from" to "import"?


#6

When we import random, the entire random module is imported. To use the methods, we must call them on the random class.

print random.randint(1, 100)

However, we do not need the entire random module, all we need is randint and to be able to call it like a function.

from random import randint
print randint(1, 100)

Continuing from the earlier interactive session...

>>> play = int(input("Enter your guess [1..100]: "))
Enter your guess [1..100]: 42
>>> spin = randint(1, 100)
>>> if play == spin:
	print ("You win ${}!".format(bet))
	dollar += bet
else:
	print ("You lose ${}.".format(bet))
	dollar -= bet

	
You lose $100.
>>> print ("Your money: ${}".format(dollar))
Your money: $0
>>>