如何使这个程序成为python中的两个播放器?
有谁能帮我改变这个程序,让两个玩家都能玩? 定义一个模拟掷骰子的过程如何使这个程序成为python中的两个播放器?,python,python-3.x,Python,Python 3.x,有谁能帮我改变这个程序,让两个玩家都能玩? 定义一个模拟掷骰子的过程 def dice(sides): # Get a random number between 1 and 6 throw=random.randint(1,6) print("score",throw) return throw #Welcome messages print("Snakes and ladders") #Main program for the game Player1=input("
def dice(sides):
# Get a random number between 1 and 6
throw=random.randint(1,6)
print("score",throw)
return throw
#Welcome messages
print("Snakes and ladders")
#Main program for the game
Player1=input("What is the name of player 1? ")
import random
position=0
#eeping it all in a loop will allow the players to keep rolling the dice until the game has finished
while position<49:
throw=input("Roll the dice")
dice1=dice(6)
dice2=dice(6)
total=dice1+dice2
#If the total of the dice thrown is not a double the player will move the total numbers of squares
if dice1!=dice2:
print("You are now on space", position+total)
position=total+position
if position>=49:
print("You have won")
if position<=0:
print("You are on square 0")
#Now I will check to see if the player has rolled a double
if dice1==dice2:
print("This is a double you will now move back",total,"spaces")
if position<=0:
print("You are on space 0")
else:
print("You are now on space", position-total)
#This means that if the vaule of dice1 is the same as dice2 then it will move back and if not it will move forward
def骰子(侧面):
#获取一个介于1和6之间的随机数
投掷=随机。随机数(1,6)
打印(“分数”,投掷)
回击
#欢迎信息
印刷品(“蛇和梯子”)
#游戏主程序
Player1=输入(“播放器1的名称是什么?”)
随机输入
位置=0
#在一个循环中保持这一切将允许玩家继续掷骰子,直到游戏结束
位置=49时:
打印(“你赢了”)
如果position您可以通过使用类实现此代码来学到很多东西
在尽可能保持代码与您正在使用的代码相似的情况下,有许多方法可以添加第二个播放器
例如,您可以添加另一个变量position\u p2
来存储玩家2的位置,并重复您在player2上对玩家1所做的相同检查,如下所示:
def dice(sides): # NOTE function names should start with verbs, e.g. roll_dice
# Get a random number between 1 and 6
throw=random.randint(1,6)
print("score",throw)
return throw
#Welcome messages
print("Snakes and ladders")
#Main program for the game
Player1=input("What is the name of player 1? ") #NOTE variables should be lower case
# Ask player2 for it's name
player2 = input("What is the name of player 2? ")
import random #NOTE imports should go at the beggining of the script
position_p1=0 # rename so that is easy to remember that it refers to p1
# store p2 position
positoin_p2 = 0
#eeping it all in a loop will allow the players to keep rolling the dice until the game has finished
while position<49:
throw_p1=input("Player 1: roll the dice") #same as above
dice1=dice(6)
dice2=dice(6)
total=dice1+dice2
#If the total of the dice thrown is not a double the player will move the total numbers of squares
if dice1!=dice2:
position_p1=total+position_p1 # you can use position_p1 += total
print("You are now on space", position_p1)
if position_p1>=49:
print("Player 1 won") # Change so that you know which player has won
if position_p1<=0:
print("You are on square 0")
#Now I will check to see if the player has rolled a double
if dice1==dice2:
print("This is a double you will now move back",total,"spaces")
position_p1 -= total
if position_p1<=0:
print("You are on space 0")
else:
print("You are now on space", position_p1)
#This means that if the vaule of dice1 is the same as dice2 then it will move back and if not it will move forward
throw_p2=input("Player 2: roll the dice")
dice1=dice(6)
dice2=dice(6)
total=dice1+dice2
#If the total of the dice thrown is not a double the player will move the total numbers of squares
if dice1!=dice2:
position_p2=total+position_p2 # you can use position_p1 += total
print("You are now on space", position_p2)
if position_p2>=49:
print("Player 2 won") # Change so that you nkow which player has won
if position_p2<=0:
print("You are on square 0")
#Now I will check to see if the player has rolled a double
if dice1==dice2:
print("This is a double you will now move back",total,"spaces")
position_p2 -= total
if position_p2<=0:
print("You are on space 0")
else:
print("You are now on space", position_p2)
#This means that if the vaule of dice1 is the same as dice2 then it will move back and if not it will move forward
def dice(侧面):#注意函数名应以动词开头,例如roll_dice
#获取一个介于1和6之间的随机数
投掷=随机。随机数(1,6)
打印(“分数”,投掷)
回击
#欢迎信息
印刷品(“蛇和梯子”)
#游戏主程序
Player1=输入(“Player1的名字是什么?”)#注意变量应为小写
#向player2询问它的名字
player2=输入(“player2的名字是什么?”)
导入随机#注意导入应该在脚本开始时进行
位置p1=0#重命名,以便容易记住它指的是p1
#存储p2位置
位置p2=0
#在一个循环中保持这一切将允许玩家继续掷骰子,直到游戏结束
位置=49时:
打印(“玩家1赢了”)#更改以便您知道哪个玩家赢了
如果位置不正确,欢迎使用堆栈溢出!请带上,四处看看,并通读,尤其是和。来自第二个链接:“请求家庭作业帮助的问题必须包括到目前为止你为解决问题所做的工作的摘要,以及对你解决问题的困难的描述。”