重新启动程序时似乎出错,无法正确重新启动。它会询问你是否想再玩一次,然后询问球员的名字,告诉你目标,然后自己重新开始。你知道吗
import random, time
#Variables
die1 = 0
die2 = 0
goal = 0
tries1 = 0
tries2 = 0
sum = 0
sum2 = 0
choice = "y"
while choice == "y":
#Asking for the player's names
player1 = input("What is your name, player1?")
player2 = input("What is your name, player2?")
#The "goal"
goal = random.randrange(5) + 1
print("The goal is:", goal)
#First while loop for the first die rolled by the first player
while die1 != goal:
die1 = random.randrange(5) + 1
time.sleep(1)
print(player1, "Your roll is:", die1)
tries1 = tries1 + 1
sum = sum + die1
time.sleep(1.5)
print("Your sum is:", sum)
#Second while loop for the second die rolled by the second player
while die2 != goal:
die2 = random.randrange(5) + 1
time.sleep(1)
print(player2, "Your roll is:", die2)
tries2 = tries2 + 1
sum2 = sum2 + die2
time.sleep(1.5)
print("Your sum is:", sum2)
time.sleep(2)
#The statement at the end of the game
print("\n\n""Player", "\t\t", "Goal", "\t\t", "# of Rolls","\t\t", "Sum of Rolls", "\n", player1, "\t\t", goal, "\t\t", tries1,"\t\t\t", sum, "\n", player2, "\t\t", goal, "\t\t", tries2, "\t\t\t", sum2)
choice = input("Would you like to play again?: (y) or (n)")
goal = 0
tries1 = 0
tries2 = 0
sum = 0
sum2 = 0
break
代替你的分手声明,写上:
if choice != 'y': break
如果用户键入了“y”,这将导致外部while循环返回到开头。否则您将跳出循环,脚本将退出。你知道吗
这可能是因为在程序结束时,不管发生什么,你都会跳出主while循环。您应该用使用choice变量的if语句来包围它。另外,最后一条语句与while内联,当第二个玩家掷骰子时,它将在该循环中执行。为了解决这个问题,你需要减少缩进,这样就可以在游戏结束时执行了。而且tries1变量被缩进了一个空格,因此python可能也会抛出某种形式的语法错误。你知道吗
相关问题 更多 >
编程相关推荐