-1

这是一个3人游戏。在这个游戏中,随机生成一个 1 到 99 之间的秘密数字。每个玩家轮流猜测这个秘密数字。玩家将被告知他的猜测是高于还是低于随机生成的数字。当其中一名玩家做出正确的猜测时,游戏将结束。出局将显示每个玩家的猜测次数和获胜者。

我已经输入了一些代码:

import random

userNumber = input("enter a number between 1 and 99:")
secretNummber = random.randint(1,99)
if userNumber > secretNumber:
    print "too high..."
elif userNumber < secretNumber:
    print "too low..."
else:
    print "you made the right guess..."
print "secret number"+str(secretNumber)
4

2 回答 2

0

您可以做的是为每一“轮”猜测使用一个循环,然后每轮取 3 个输入:

import random

secretNumber = random.randint(1,99)

while True:
    for player in range(1,4):
        userNumber = input("Player %s: enter a number between 1 and 99:" % player)

        if userNumber > secretNumber:
            print "too high..."
        elif userNumber < secretNumber:
            print "too low..."
        else:
            print "Player %s, you made the right guess..." % player
            exit(0)
于 2013-07-25T01:34:12.103 回答
0
import random
from itertools import cycle

secretNumber = random.randint(1,99)

for player in cycle(["Tom", "Dick", "Harry"]):
    userNumber = int(raw_input("{}: enter a number between 1 and 99:".format(player)))

    if userNumber > secretNumber:
        print "too high..."
    elif userNumber < secretNumber:
        print "too low..."
    else:
        print "{}, you made the right guess...".format(player)
        break
于 2013-07-25T01:53:36.543 回答