c语言转python急急急

2024-11-22 11:45:13
推荐回答(1个)
回答1:

import random

def player(tol):
    inp = 100
    while inp > 2 or inp < 1 or inp + tol > 50:
        inp = int(raw_input("Please count (1 or 2): "))
    return inp + tol

def computer(tol):
    if tol + 1 == 50 or (tol + 1) % 3 == 0:
        tol += 1
    elif tol + 2 == 50 or (tol + 2) % 3 == 0:
        tol += 2
    else:
        tol += int(random.random() * 2) + 1
    print("Computer count: {}".format(tol))
    return tol

def main():
    tol = 0
    print("* * * * * * * * Grab Fifty * * * * * * * *")
    print("Game Begin")
    whose_turn = player
    if random.random > .5:
        whose_turn = computer
    while tol != 50:
        tol = whose_turn(tol)
        if tol == 50:
            if whose_turn == player:
                print("You Win!")
            else:
                print("You Lose!")
            break
        whose_turn = player if whose_turn == computer else computer
    print("* * * * * * * * Game Over * * * * * * * *")

if __name__ == "__main__":
    main()