import random as r
def main():
        roll1 = r.randint(1,6)
        roll2 = r.randint(1,6)
        point= 4,5,6,8,9,10
        sum= roll1 + roll2
        print("phase 1")
        print ("You rolled:", roll1, "and", roll2)
        if sum == 2 or sum== 3 or sum == 12:
            print("Game Over. You lose" )
        elif sum == 7 or sum == 11:
            print("You won!Game over")
        elif sum in [4,5,6,8,9,10]:
            print("point", roll1+roll2)
        while sum == point:
                print("phase 2")
        if sum== 7:
                print("you lose")
        elif sum== point:
                print("you win!")
        else: sum !=7 and sum !=point

Beginner coder and having some trouble with a while loop for this assignment making a simple craps game. This is what I have so far...

Recommended Answers

All 5 Replies

I think you should also call your main function ;) Luckyly the sum can not be the 6-tuple of numbers as the sum or point variables do not change inside the while at line 14. sum is also bad name for variable as it is used for function to do sum of sequence. Line 21 does nothing

What is the while loop supposed to do?? Right now it just prints. Note that

    while sum == point:

will never be True because you are comparing an integer and a tuple.

Sorry I guess I should explain after the player rolls the sum of the two die that is 4,5,6,8,9,10 that becomes a point then the game proceeds thru phase 2. So I was trying to create a loop that would do that

Ah, in that case, you want

       while sum in point:

It may be better, too, if you were to declare point explicitly as a list or tuple; as it is, it becomes a tuple anyway, because of the assignment unpacking rules, but it would be better to explicit.

       point = (4,5,6,8,9,10)

I should add that sum isn't a great variable name, because there is a built-in function of the same name, which gets masked as a result of using that as a a variable. A better variable name might be result. EDIT: Ah, PyTony already mentioned that, I see.

Finally, most of what appears to be meant as the body of the loop is in fact outside of it. you need to indent this part correctly, or else the only thing you'll get is an infinite series of the phrase "Phase 2". Also, you never generate a new pair of dice rolls, so it never ends, anyway. It would make more sense to have the rolls generated with a small function that returns two values:

def rollDice():
    return r.randint(1,6), r.randint(1,6)

Then call it both at the start of the main() function, and each pass of while: loop:

        print("phase 2")
        firstResult = result
        while result in point:
            roll1, roll2 = rollDice()
            if result == 7:
                print("you lose")
            elif result == firstResult:
                print("you win!")
            else:
                print ("roll again.")

Schol-R-LEA you have a couple of mix up(typo error)
while sum in point: while result in point:
Should be if sum in point: no in statement in while loop.

Sorry I guess I should explain after the player rolls the sum of the two die that is 4,5,6,8,9,10 that becomes a point then the game proceeds thru phase 2. So I was trying to create a loop that would do that

Here are som code you can look at.
Se how i have small functions that dos a specific task,and not one big main() function with all code.
You only reach phase 2 function if dice sum is in (4,5,6,8,9,10).
Where you can code some stuff for phase 2.

from random import randint

def dice():
    '''Return sum of two dice trown'''
    return(sum(randint(1,6) for item in range(2)))

def check_score(dice_sum, point):
        if dice_sum in point:
            return dice_sum
        return 0

def phase_2():
    print('welcome to phase_2')
    input('Press enter to exit')

if __name__ == '__main__':
     point = (4,5,6,8,9,10)
     dice_sum = dice()
     check_score = check_score(dice_sum,point)
     #---
     print('Phase_1 your dice sum has to be equal one of these numers(4,5,6,8,9,10)\n')
     if check_score == 0:
        print('Wrong dice sum, Game over')
     else:
        print('Your sum of dice trow was {}'.format(check_score))
        phase_2()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.