这里是初学者程序员!对于我的一个班级,我必须创建一个骰子游戏,当玩家或计算机得分达到5时该骰子游戏停止,但是当一个人确实达到所需得分时,它会不断提示玩家滚动而不是打印winString或lostString。
编辑:对不起,格式化,这是我在这里的第一篇文章,不确定如何解决。但是多亏您的评论,我才能成功!
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! *"
while playerScore <= 5 or computerScore <= 5:
行应改用and
,因为您希望在其中一个为假]时中断它,因此,如果其中一个分数为>6
,则它会与and
中断。除此之外,看起来还不错吗?