使用if语句减少“玩家”计数

2024-04-24 06:33:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为一个游戏编写一个脚本,在这个游戏中,用户必须猜测计算机生成的值。其目的是减少玩家数量,直到其值为1时结束

import random 
import time

players = input("Let's play Five's! How many are you?:" )
#print("you are", players, "players?") #test number of players
players = int(players)


if players <=1:
    print("Game Over")

else:
    while players >= 1:
        players = players+1

#Decide possible values than can be chosen
        options = [] #Possible options
        for i in range(0,players):
            x = i * 5
            options.append(x)
        print("Your choices are", options)

#Playing the game
#Each turn
        guess = random.choice(options)
        print("Computer has chosen", int(guess))
        count_down = 3
        while (count_down):
            print(count_down)
            time.sleep(1)
            count_down -=  1
        choice = input("Guess:")
        choice = int(choice)
        if choice not in options: #If choice isn't a multiple of 5
            input("Not allowed, choose again:")
        elif choice in options and choice != guess: #Valid choice but wrong
                print("Wrong")                      #so player is still in the
        else:                                       #game
            choice = int(choice)
            if choice == guess: #Correct choice so player leaves game
                print("You're Out.") # this should reduce the player count
        players -=1

我已经包括了打印计算机生成值的行,这样我可以给出正确的猜测,但即使猜测正确,也不会减少玩家数量,因此游戏将无限继续


Tags: theingame游戏inputifcountare