## sijei

Im a beginner programmer this semester, and python is my first language. I can do the homework but am naturally curious at spiffing up my programs to help me understand how to have many things working at once in a program.

I have some ideas but have not been able to get clear direction from the text or my web searches so far. Any tips or suggestions would be helpful and appreciated.

First problem- Craps player
I have the game working properly. But I would like to make the player hit enter to initiate each of the rolls. Right now the program just starts and simulates a complete turn of craps.

Second- Math tutor
We were to write a program to ask the user to multiply 2 random number together between 1 and 9. It was then to check the answer and give an output message. I added a correct answer count to the final output (the program was to run for ten questions. But ran into trouble getting the correct# and count# to divide giving a percentage. And I also wanted to add a timer that would start upon the first question being asked, ending with the last answer. Printing this out at the final output as well.

## pyTony 888

You should post runnable code of what you have. First the first program and remember to push Code before pasting.

## sijei

You should post runnable code of what you have. First the first program and remember to push (CODE) before pasting.

Thanks for the tip

Craps game code

``````# Variables and Set declarations

import random
n= random.randint(1,6) # This is used in first roll
x= random.randint(1,6) # Also first roll
k= x + n

lst1 = [1, 4, 5, 6, 8, 9, 10]
lst2 = [7, 11]
lst3 = [2, 3, 12]

o= random.randint(1,6) # This is the rolls after the point is determined
p= random.randint(1,6)

#---------------------------------------------------------------------------------------------------------------------------------
#----Intro to the game is the first roll

print "You wanna play craps?"
print "Lucky rollers first throw is a " ,k
#---Here a 2,3,or 12 loses; 7 or 11 wins; any other throw becomes the "point" in this program "k"
if k in lst2:
print "HOT DOG!! YOU WIN!!"

else:
if k in lst3:
print "Sorry pal, your a loser"
else:
print "The point is: " ,k
print "Roll again player "

# Here is another throw. This is the repeated until this throw is equal to the "point" or 7
m= o + p
print "Your throw is a " ,m

while m != k :
o= random.randint(1,6)
p= random.randint(1,6)
m= o + p
print "Your throw is a " ,m
if m == 7:
break

if m == k:
print "Player wins"
else:
print "Player Craps Out!!"
``````

## sijei

Here's the Math tutor

``````# Variable Declarations

import random

x= random.randint(1,9)
y= random.randint(1,9)

b= random.randint(1,9)
c= random.randint(1,9)

# Start the program
#------------------------------------------------------------------------------

print "Lets try some math problems!!"

correct = 0
print "Multiply the following two numbers: ",x ,y

a = x * y

if n == a:
correct = correct + 1
print "You are correct!!"
else:
print "Sorry, the answer was ",a

count = 1

while count <= 9:
b= random.randint(1,9)
c= random.randint(1,9)

print "Multiply the following two numbers: ",b ,c

g = b * c

if m == g:
correct = correct + 1
print "You are correct!!"

else:
print "Sorry, thats not correct. The answer was ", g

count = count + 1

print "That was fun!!"
print "Number of correct answers: " ,correct``````

## sijei

With the math tutor I had another though. Right now the way the program asks the question like this Multiply the following two numbers: ,x ,y

Id like it to look like this Multiply ,x times ,y

I'm not sure how to print this out properly.

## pyTony 888

You got random.randint(1,6) in your code three times I would define function for that:

``````def roll_dice():
return random.randint(1,6)``````

It is super trivial, but makes quite a lot for readability and maintability of code.

For waiting input you can just change line 30:

``raw_input("Roll again player by Hitting Enter")``

And similarly line 40.
I do not see lst1 used. What is it?

## sijei

lst 1 was used in an earlier version of the program. It represents all possible values for k. Not needed in this program at all actually.

Thats a great tip for a beginning programmer, I really should only leave the relevant variables in the code for clarity sake.

I like the def roll dice tip and raw input ideas, and am gonna mess with those.

## woooee 814

Id like it to look like this Multiply ,x times ,y

``````print "Multiply", x, "times", y
print "Multiply %d times %d" % (x, y)``````

## sijei

``````print "Multiply", x, "times", y
print "Multiply %d times %d" % (x, y)``````

This is great, although I feel slightly slow not figuring it out . I had tried various way of writing it but my commas were definitley off.

## sijei

final codes for the two programs that i handed in. thanks all for the tips.

``````import random
n= random.randint(1,6)
x= random.randint(1,6)
k= x + n

lst1 = [7, 11]
lst2 = [2, 3, 12]

o= random.randint(1,6)
p= random.randint(1,6)

#----------------------------------------------------------------------------

print "Lets Play Craps!"
print "New Shooter to the Line!!"
z= raw_input("-- Hit Enter to Roll --")

print "Lucky rollers first throw is a " ,k

if k in lst1:
print "HOT DOG!! YOU WIN!!"
else:
if k in lst2:
print "Sorry shooter, your a loser!"
else:
print "The point is " ,k
print "Roll again player "
z= raw_input("-- Hit Enter to Roll --")

m= o + p
print "Your throw is " ,m

if m==7:
print "Player Craps Out!!"

else:
while m != k :
z= raw_input("-- Hit Enter to Roll --")
o= random.randint(1,6)
p= random.randint(1,6)
m= o + p
print "Your throw is a " ,m

if m == 7:
break

if m == k:
print "Player Wins!!"
else:
print "Player Craps Out!!"``````
``````# Math Tutor
#------------------------------------------------------------------------------------------------------------------

# Variable Declarations

import random

x= random.randint(1,9)
y= random.randint(1,9)

b= random.randint(1,9)
c= random.randint(1,9)

# Start the program
#------------------------------------------------------------------------------

print "Lets try some math problems!!"

correct = 0
print "What is:", x, "*",y

a = x * y

if n == a:
correct = correct + 1
print "You are correct!!"
else:
print "Sorry, the answer was ",a

count = 1

while count <= 9:
b= random.randint(1,9)
c= random.randint(1,9)

print "What is: ", b, "*",c

g = b * c

if m == g:
correct = correct + 1
print "You are correct!!"

else:
print "Sorry, thats not correct. The answer was ", g

count = count + 1

print "That was fun!!"

## pyTony 888

My craps version from your original idea:

``````import random
point = 0
winning_roll = set([7, 11])
losing_roll = set([2, 3, 12])

def roll_dice():
return random.randint(1,6)

def announce(throw):
""" Announce throw and fix the point, return False if finnished True otherwise """
global point
total = sum(throw)
if not point:
if total in winning_roll:
print "HOT DOG!! YOU WIN!!"
return False
if total in losing_roll:
print "Sorry pal, you are a loser"
return False
else:
print "The point is: " , total
point = total
return True
elif total == point:
print "You won!"
return False
elif total == 7:
print "You craped out!"
return False
else:
return True

def roll_pair():
throw = roll_dice(), roll_dice()
print "Your throw is %i + %i =" % (throw), sum(throw)
return throw

#---Here a 2,3 or 12 loses; 7 or 11 wins; any other throw becomes the "point" in this program
while announce(roll_pair()):
raw_input("Roll again player by Hitting Enter!")``````