【发布时间】:2020-07-28 21:42:06
【问题描述】:
我是一个新手,正在制作一个用于学习目的的小游戏。 游戏根据用户的输入掷骰子。
我遇到问题的部分是我想检查列表“rolls”中的模式
模式包括:
所有骰子的值相同,边数 >=4 示例 [1, 1, 1, 1]
-
至少一半的骰子 >= "average_sum",条件是列表必须有 >= 5 个骰子
- 示例如果 avg_sum = 2 并且 rolls = [2,3,4,1,1,] 如果为真,则将 user_Score 乘以 5
所有骰子都是不同的值,条件是骰子数 > 4 和边数 > 骰子数 [10,11,12,13,14]
没有模式匹配。 -> 将 user_Score 乘以 1
number_dice = int( input( "How many dice are you using? Must be between 3-6 inclusive" ) )
faces = int( input( "how many sides are on your die? enter a number between 2-20 inclusive: "))
# Set range for number of dice
#generate a random number between 1 and faces
#Add dice_roll to the list
rolls = []
for die in range(number_dice):
dice_roll = random.randint(1, faces)
rolls.append(dice_roll)
#print the score from each dice rolled
print("You have rolled: " + str(rolls))
#calculate sum of score
sum = sum(rolls)
#calculate the average and round to the nearest integer
average_sum = round(sum / number_dice)
print("These die sum to: " + str(sum) + " and have an average value of: " + str(average_sum))
#Calculate the max possible score
max_score = (number_dice * faces)
#calculate the users score
user_score = float( sum / max_score )
print("your max possible score is " + str(max_score))
print("your score is " + str(user_score))
#-----------------------------------------------------------------------------------
#now calculate the bonus factor
#Check if the list "rolls" contains the same value for each index
if rolls == {repeatingvalues???} and rolls {number_dice>=4}:
user_Score * 10
elif rolls == {half of dice > average} and {number_dice >=5}:
user_Score * 5
elif rolls == {all dice have different values} and { number_dice > 4}{faces> number_dice}:
user_score * 8
else:
user_score * 1
不确定如何使此语句在列表中搜索模式^^^^^^
【问题讨论】:
标签: python python-3.x list