我如何创建一个在玩家之间交替的游戏?

时间:2014-09-22 18:20:42

标签: python

我希望猜测我的数字游戏在用户猜测和Al之间交替,但我不知道该怎么做。我已经猜到了我的号码游戏代码,但我不知道要添加什么来让它从用户更改为计算机。

#Guess my number

import random

print ("Welcome to the guess my number game")
print ("***********************************")
print ("I will choose a random number between 1-100")
print ("and you must guess what it is.")
print ()

number = random.randint(1,100)
guesses = 0
guess = 0

while guess != number:
    guess = int(input("Take a guess: "))
    guesses=guesses+1

    if guess > number:
        print("Lower...")
        continue

    elif guess < number:
        print ("Higher...")

    else:
        print ("Well done you have guessed my number and it took you", guesses,"guesses!")

input ("\n\nPress enter to exit.")

1 个答案:

答案 0 :(得分:0)

guesses = [[],[]]
for player in itertools.cycle([0,1]):
    if player == 0:
         guess = int(input("Take a guess: "))
    else: 
         guess = computer_guess()
    guesses[player].append(guess)
    if guess == number:

       break
    if guess > number:
        print("Lower...")
        continue

    elif guess < number:
        print ("Higher...")

print "Player %d wins"%player
print "It Took %d guesses"%len(guesses[player])

也许你在寻找什么