gpt4 book ai didi

python - 如何根据掷骰子的得分对名字进行排序

转载 作者:行者123 更新时间:2023-12-01 05:49:01 26 4
gpt4 key购买 nike

我正在开发一个程序,它生成三个不同的整数并将它们分配给单独的值,以便可以决定它是第一,第二和第三。在这种情况下,我假设三个不同的玩家,每个人都掷一个“十面骰子”。最高的应该先转,第二高的应该转第二,第三的应该最后转。一切似乎都进展顺利,但现在我有了自己的值(value)观,我不知道如何安排它们,以便我可以开始让玩家轮流玩。如果有任何意见,我将不胜感激。

这是我到目前为止整理的代码:

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()

最佳答案

就像 Lattyware 提到的那样,您正在重复一些代码。它不仅使它变得太忙而无法阅读,而且还提供了搞乱逻辑的机会(如果您在粘贴后忘记更改变量)。因此,将重复代码放入函数中是一个很好的做法。

关于您的代码,我考虑了两个玩家是否掷出相同的数字。在这种情况下,程序将再次滚动,直到滚动出新的数字。

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 initialRoll(player):
"""Roll the dice for the given player"""

playerRoll = random.randint(1, 10)
print ('<Ten-sided dice roll> ')
print ('---------------------------------')
print (player + ' rolls ' + str(playerRoll))
print()
return playerRoll


def startFightRoll():
"""Determine the order of the players."""

time.sleep(.5)
print('Okay, let\'s roll a ten-sided die to see who gets to go first!')
print()

// Temporarily store the rolls. The key is the roll number and the value is the
// player who rolled it
order = {}
for player in [playerOne, playerTwo, playerThree]:
playerRoll = initialRoll(player)

# Let's make sure that two players didn't roll the same number, if they did
# then let's roll a new number
while playerRoll in order:
print ('OH No! That number has already been rolled. Let\'s roll again')
playerRoll = initialRoll(player)

order[playerRoll] = player
time.sleep(2)

# Sort the keys (which are the numbers rolled), then create a new list with order
# of who should go first
return [order[roll] for roll in sorted(order.keys(), reverse=True)]

rollOrder = startFightRoll()
print ('The order of the players are: ' + ', '.join(rollOrder))

关于python - 如何根据掷骰子的得分对名字进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15075351/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com