我可以使用它,但是在创建列表/调用列表时很糟糕。谁能将其压缩为列表形式? (不是为了做作业,已经完成了,只是为了学习机会/练习,所以不要着急。)我假设我的count变量可以放入列表中,并在for循环中和最终打印时调用。
这是一个骰子游戏,该游戏跟踪投掷2个骰子并加在一起后一个数字出现多少次。太容易了,我只是在理解列表,所以对代码的解释会很有意义,我的教科书对此主题的阐述还不够。首先发表在栈上,请原谅任何混乱的行话或规则。谢谢!
import random
count2 = 0
count3 = 0
count4 = 0
count5 = 0
count6 = 0
count7 = 0
count8 = 0
count9 = 0
count10 = 0
count11 = 0
count12 = 0
rolls = int(input("How many times would you like to roll? "))
for i in range(rolls):
die1 = random.randint(1, 6)
print("Roll number 1 = " + str(die1))
die2 = random.randint(1, 6)
print("Roll number 2 = " + str(die2))
total = die1 + die2
print(total)
if total == 2:
count2 += 1
elif total == 3:
count3 += 1
elif total == 4:
count4 += 1
elif total == 5:
count5 += 1
elif total == 6:
count6 += 1
elif total == 7:
count7 += 1
elif total == 8:
count8 += 1
elif total == 9:
count9 += 1
elif total == 10:
count10 += 1
elif total == 11:
count11 += 1
elif total == 12:
count12 += 1
print("You rolled " + str(count2) + " 2's")
print("You Rolled " + str(count3) + " 3's")
print("You Rolled " + str(count4) + " 4's")
print("You Rolled " + str(count5) + " 5's")
print("You Rolled " + str(count6) + " 6's")
print("You Rolled " + str(count7) + " 7's")
print("You Rolled " + str(count8) + " 8's")
print("You Rolled " + str(count9) + " 9's")
print("You Rolled " + str(count10) + " 10's")
print("You Rolled " + str(count11) + " 11's")
print("You Rolled " + str(count12) + " 12's")
答案 0 :(得分:2)
您可以在此处使用列表或字典。我倾向于使用字典,我认为它最能代表您要在这里使用的稀疏数据结构(count
列表的第一个元素是什么?它将始终为零,但不是吗?真的是 nothing 吗?零没有被掷过一次还是完全不能被滚出是否更有意义?)
最好将字典定义为:
counts = {}
# You can also generalize your rolling 2d6!
def roll_dice(num_dice, sides):
total = 0
for _ range(num_dice):
dieroll = random.randint(1, sides)
total += dieroll
return total
for _ in range(rolls):
roll = roll_dice(2, 6)
counts.setdefault(roll, 0) += 1 # this is using dict.setdefault
for roll, count in sorted(counts.items()):
print("You rolled {} {}s".format(count, roll))
您也可以使用collections.Counter
来完成此操作。
rolls = [roll_dice(2, 6) for _ in num_rolls]
# this will generate a list like [3, 12, 6, 5, 9, 9, 7, ...],
# just a flat list of rolls.
result = collections.Counter(rolls)
答案 1 :(得分:1)
稍微修改了您的代码,我认为如果您使用字典,那么它将易于使用和理解。
import random
# count2 = 0
# count3 = 0
# count4 = 0
# count5 = 0
# count6 = 0
# count7 = 0
# count8 = 0
# count9 = 0
# count10 = 0
# count11 = 0
# count12 = 0
count = {i: 0 for i in range(2,13)}
rolls = int(input("How many times would you like to roll? "))
for i in range(rolls):
die1 = random.randint(1, 6)
print("Roll number 1 = " + str(die1))
die2 = random.randint(1, 6)
print("Roll number 2 = " + str(die2))
total = die1 + die2
print(total)
# if total == 2:
# count2 += 1
# elif total == 3:
# count3 += 1
# elif total == 4:
# count4 += 1
# elif total == 5:
# count5 += 1
# elif total == 6:
# count6 += 1
# elif total == 7:
# count7 += 1
# elif total == 8:
# count8 += 1
# elif total == 9:
# count9 += 1
# elif total == 10:
# count10 += 1
# elif total == 11:
# count11 += 1
# elif total == 12:
# count12 += 1
count[total] += 1
# print("You rolled " + str(count2) + " 2's")
# print("You Rolled " + str(count3) + " 3's")
# print("You Rolled " + str(count4) + " 4's")
# print("You Rolled " + str(count5) + " 5's")
# print("You Rolled " + str(count6) + " 6's")
# print("You Rolled " + str(count7) + " 7's")
# print("You Rolled " + str(count8) + " 8's")
# print("You Rolled " + str(count9) + " 9's")
# print("You Rolled " + str(count10) + " 10's")
# print("You Rolled " + str(count11) + " 11's")
# print("You Rolled " + str(count12) + " 12's")
for i in range(2,13):
print("You rolled " + str(count[i]) + " "+i+"'s")
答案 2 :(得分:0)
创建11个零的列表:
counts = [0] * (12 - 2 + 1)
要增加计数:
counts[total - 2] += 1
现在在一起:
import random
def roll_dice():
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
total = die1 + die2
print(f"Roll number 1 = {die1}")
print(f"Roll number 2 = {die2}")
print(total)
return total
min_count = 2
counts = [0] * (12 - min_count + 1)
rolls = int(input("How many times would you like to roll? "))
for i in range(rolls):
total = roll_dice()
counts[total - min_count] += 1
print('\n'.join(f"You rolled {x} {i + min_count}'s"
for i, x in enumerate(counts)))
答案 3 :(得分:0)
我刚刚对您的代码做了一点编辑:
select ll.line_id, ll.line_value, bb.branch_unit_cd,ll.unit
from
(
select l.*, b.parent
from line l
left outer join Branch b
on ( l.unit = b.branch_unit_cd )
) ll inner join
Branch bb
on ( ll.parent = bb.branch_unit_id );
LINE_ID LINE_VALUE BRANCH_UNIT_CD UNIT
------- ---------- -------------- ----
1 123456 FFF EEE
答案 4 :(得分:0)
在这种情况下,我会使用字典。 或特别是defaultdict。
import random
from collections import defaultdict
roll_scores = defaultdict(int)
rolls = 10
for _ in range(rolls):
die1 = random.randint(1, 6)
die2 = random.randint(1, 6)
total = die1 + die2
print("Roll 1: ", die1)
print("Roll 2:", die2)
print("Total:", total)
roll_scores[total] +=1
for k in roll_scores:
print("You rolled {} {}'s".format(roll_scores[k], k))
但是,如果要使用列表,则概念几乎相同。 将roll_scores更改为13个项目列表(0到12):
roll_scores = [0]*13
并在最后更改打印:
for i in range(len(roll_scores)):
print("You rolled {} {}'s".format(roll_scores[i], i))