Python - Игра в кости не останавливается при достижении назначенного счета - PullRequest
0 голосов
/ 29 марта 2020

Начинающий программист здесь! Для одного из моих классов я должен создать игру в кости, которая останавливается, когда игрок или компьютер набирает пять очков, однако, когда один человек достигает желаемого результата, он продолжает предлагать игроку бросить вместо того, чтобы печатать winString или loseString.

РЕДАКТИРОВАТЬ: Извините за форматирование, это мой первый пост здесь и не уверен, как это исправить. Но благодаря вашим комментариям я получил его на работу!

import random
from random import randint
import time

playerName = input("Please enter your name ") 

def diceRoll(num_sides=6):
    """Returns number between 1 and 6 (inclusive)"""
    return random.randint(1,num_sides)

def playGameOfDice(playerName):
    print("Hello,",playerName,". Welcome to game of dice! First to five points wins!")
    playerScore = 0
    computerScore = 0
    while playerScore <= 5 or computerScore <= 5: 
        print("The current score is: ",playerName, playerScore, " , computer ",computerScore)
        input("Please press 'Enter' to roll.")
        playerRoll = diceRoll()
        print("Your randomly selected number is.......... "+str(playerRoll)+".") 
        time.sleep(2)
        computerRoll = diceRoll()
        print("Computer's randomly selected number is.......... "+str(computerRoll)+".")
        time.sleep(2)
        for round in range(1):
            if playerRoll > computerRoll:
                print("You Win!")
                playerScore += 1
            elif playerRoll == computerRoll:
                print("Tie Game!")
                computerScore += 1
            else:
                print("You Lose :(")
                computerScore += 1
    if playerScore > computerScore == playerWins:
        return True
    else:
        return False




playerWins = playGameOfDice(playerName)

if playerWins == True:
    winnerString = "*  " + playerName + " Wins!  *"
else:
   winnerString = "*  Good luck next time! Thanks for playing!  *"

1 Ответ

0 голосов
/ 30 марта 2020

В строке while playerScore <= 5 or computerScore <= 5: вместо этого следует использовать and, поскольку вы хотите, чтобы она сломалась, когда либо один из них ложный , поэтому, если любой из баллов равен >6, он будет разрываться с and. Кроме того, это кажется нормальным?

...