我正在开发一个程序,该程序生成三个不同的整数并将它们分配给单独的值,以便可以决定它是第一个、第二个和第三个。在这种情况下,我假设三个不同的玩家,每个人掷一个“十面骰子”。最高的应该第一回合,第二高的应该第二,第三应该最后。一切似乎都很好,但现在我有了自己的价值观,我不知道如何安排它们,以便我可以开始让玩家轮流玩。我会很感激任何意见。
这是我到目前为止整理的代码:
import sys
import os
import random
import time
os.system('clear')
print ('Welcome! Please type Player 1\'s name!: ')
playerOne = input()
print ('Okay! Please type Player 2\'s name!: ')
playerTwo = input()
print ('Fantastic! Finally, please type Player 3\'s name!: ')
playerThree = input()
os.system('clear')
print()
time.sleep(2)
def startFightRoll():
playerOneRoll = random.randint(1,10)
time.sleep(.5)
print('Okay, let\'s roll a ten-sided die to see who gets to go first!')
print()
time.sleep(2)
print ('<Ten-sided dice roll> ')
print ('---------------------------------')
print (playerOne + ' rolls ' + str(playerOneRoll))
print()
print()
playerTwoRoll = random.randint(1,10)
time.sleep(2)
print ('<Ten-sided dice roll> ')
print ('---------------------------------')
print (playerTwo + ' rolls ' + str(playerTwoRoll))
print()
print()
playerThreeRoll = random.randint(1,10)
time.sleep(2)
print ('<Ten-sided dice roll> ')
print ('---------------------------------')
print (playerThree + ' rolls ' + str(playerThreeRoll))
startFightRoll()
响应以下线程;
好的,所以我是新手,所以请原谅我的代码 - 它效率不高,我仍然习惯了。我在其中添加了一些部分以解决 1)玩家在名称字段中不输入任何内容,以及 2)在掷骰子时生成平局。我还创建了一个按降序排列的掷骰子列表,但现在我需要找到一种方法将掷骰子关联回生成它的用户。非常感谢任何有关如何正确执行此操作的指示;
import sys
import os
import random
import time
os.system('clear')
def playerOneName():
global playerOne
playerOne = input()
if len(playerOne) < 1:
print('Please enter your name, Player 1!')
playerOneName()
def playerTwoName():
global playerTwo
playerTwo = input()
if len(playerTwo) < 1:
print('Please enter your name, Player 2!')
playerTwoName()
def playerThreeName():
global playerThree
playerThree = input()
if len(playerThree) < 1:
print('Please enter your name, Player 3!')
playerThreeName()
os.system('clear')
print()
time.sleep(2)
def startFightRoll():
global playerOneRoll
global playerTwoRoll
global playerThreeRoll
playerOneRoll = random.randint(1,10)
time.sleep(.5)
print('Okay, let\'s roll a ten-sided die to see who gets to go first!')
print()
time.sleep(2)
print ('<Ten-sided dice roll> ')
print ('---------------------------------')
print (playerOne + ' rolls ' + str(playerOneRoll))
print()
print()
playerTwoRoll = random.randint(1,10)
time.sleep(2)
print ('<Ten-sided dice roll> ')
print ('---------------------------------')
print (playerTwo + ' rolls ' + str(playerTwoRoll))
print()
print()
playerThreeRoll = random.randint(1,10)
time.sleep(2)
print ('<Ten-sided dice roll> ')
print ('---------------------------------')
print (playerThree + ' rolls ' + str(playerThreeRoll))
if playerOneRoll == playerTwoRoll:
print ('There\'s a tie, rolling again!')
time.sleep(3)
os.system('clear')
startFightRoll()
if playerOneRoll == playerThreeRoll:
print ('There\'s a tie, rolling again!')
time.sleep(3)
os.system('clear')
startFightRoll()
if playerTwoRoll == playerThreeRoll:
print ('There\'s a tie, rolling again!')
os.system('clear')
time.sleep(3)
startFightRoll()
O = [playerOneRoll, playerTwoRoll, playerThreeRoll]
O = sorted(O, reverse = True)
print (O)
print ('Welcome! Please type Player 1\'s name!: ')
playerOneName()
print ('Okay! Please type Player 2\'s name!: ')
playerTwoName()
print ('Fantastic! Finally, please type Player 3\'s name!: ')
playerThreeName()
os.system('clear')
startFightRoll()