简体   繁体   中英

Python Traceback (most recent call last)

Craps is a popular dice game played in casinos.

Write a python program to play a variation of the game as follows: Roll two dice. Each die has six faces representing values 1, 2, 3, 4, 5, and 6. Check the sum of the two dice. If the sum is 2, 3, or 12 (called craps), you lose; if the sum is 7 or 11 (called natural), you win; if the sum is another value (ie, 4, 5, 6, 8, 9, or 10), a point is established and you continue to roll the dice until either a 7 or the same point value is rolled. If 7 is rolled, you lose and if you roll the point value you win.

    import random

def rollDice():
    dice1=random.randint(1,6)
    dice2=random.randint(1,6)
    return dice1, dice2
    
def determine_win_or_lose(dice1,dice2):
    dice1=random.randint(1,6)
    dice2=random.randint(1,6)
    sum=dice1+dice2
    print("You rolled %d + %d = %d"%(dice1,dice2,sum))
    if sum==2 or sum==3 or sum==12: 
        lose=lose+1 
    elif sum==7 or sum==11: 
        win=win+1 
    else:
        print("point is %d"%(sum))
        determinePointValueResult()
        p=sum 
        print("You rolled",dice1,"+",dice2,"=",sum) 
        p1=p 
        if sum==p: 
            win=win+1 
            print("You Win") 
            print("\n\n") 
        else: 
            lose=lose+1 
            print("You lose") 
            print("\n\n") 
    return sum
    
def determinePointValueResult(point):
    while sum!=7 and sum!=point:
        rollDice()
        dice1=random.randint(1,6) 
        dice2=random.randint(1,6) 
        sum=dice1+dice2
        if sum == point:
            win=win+1
            break
        elif sum == 7:
            lose=lose+1
            break
        print("You rolled",dice1,"+",dice2,"=",sum)
    return sum
   
if __name__ == "__main__":
    
    win=0
    lose=0
    nmbr=input("How many games do you want to play > ")
    for I in number:
        rollDice()
        determine_win_or_lose(win,lose)
        if win:
            win=win+1
            print('You win')
            break
        else:
            lose=lose+1
            print('You lose')
            break
    print(win,"Wins and",lose,"losses")```
import random

def rollDice():
    dice1=random.randrange(1,7)
    dice2=random.randrange(1,7)
    sum=dice1+dice2
    return sum
def determinePointValueResult(point):
    sum = 0
    while True:
        sum = determine_win_or_lose()
        if sum == point:
            print("You won!")
            break
        elif sum == 7:
            print("You lose!")
            break
        else:
            continue
def determine_win_or_lose():
    sum = 0
    dice1=0
    dice2=0
    dice1=random.randint(1,6)
    dice2=random.randint(1,6)
    sum=dice1+dice2
    print("You rolled %d + %d = %d"%(dice1,dice2,sum))
    return sum
if _name_ == "_main_":
    nmbr=input("How many games do you want to play > ")
    for i in nmbr:
        rollDice()
        determine_win_or_lose()
        win=0 
        lose=0
        print("")
        for i in range(1,7): 
            a=random.randrange(1,7) 
            b=random.randrange(1,7) 
            c=a+b 
            if c==7 or c==11: 
                win=win+1 
                print("You rolled",a,"+",b,"=",c) 
                print("You Win") 
                print("\n\n") 
            elif c==2 or c==3 or c==12: 
                lose=lose+1 
                print("You rolled",a,"+",b,"=",c) 
                print("You Lose") 
                print("\n\n") 
            elif c==4 or c==5 or c==6 or c==8 or c==9 or c==10: 
                p=c 
                print("You rolled",a,"+",b,"=",c) 
                p1=p 
                while c!=7: 
                    print("point is",p1) 
                    a=random.randrange(1,7) 
                    b=random.randrange(1,7) 
                    c=a+b 
                    print("You rolled",a,"+",b,"=",c) 
                    if c==p: 
                        win=win+1 
                        print("You Win") 
                        print("\n\n") 
                        break 
                    elif c==7: 
                        lose=lose+1 
                        print("You lose") 
                        print("\n\n") 
                        break 
                    else: 
                        continue 
                    
print("Game results: ",win," and ",lose,"losses")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM