基本上,我的任务是模拟掷骰子,并根据掷出的物品并排打印出来。骰子每个占用5行,所以我需要一种方法在所有5行的另一个骰子结果旁边打印出所有5行。
我一直在尝试的程序尝试将函数转换为常规字符串,但没有用,我还尝试将,end =''放到函数本身中,然后将它们直接放在下面,但我没有那里也好运。我只在第一个骰子为1的情况下发布。
import random
def roll_dice():
return random.choice(['1','2','3','4','5','6'])
def display_die1():
print("+-------+")
print("| |")
print("| * |")
print("| |")
print("+-------+")
def display_die2():
print("+-------+")
print("| * |")
print("| |")
print("| * |")
print("+-------+")
def display_die3():
print("+-------+")
print("| * |")
print("| * |")
print("| * |")
print("+-------+")
def display_die4():
print("+-------+")
print("| * * |")
print("| |")
print("| * * |")
print("+-------+")
def display_die5():
print("+-------+")
print("| * * |")
print("| * |")
print("| * * |")
print("+-------+")
def display_die6():
print("+-------+")
print("| * * * |")
print("| |")
print("| * * * |")
print("+-------+")
def main():
pic1, pic2, pic3, pic4, pic5, pic6 = display_die1(), display_die2(), display_die3(), display_die4(), display_die5(), display_die6()
print(f"Craps: A Popular Dice Game")
prompt = input(f"Press <Enter> to roll the dice.")
if prompt == (""):
x = roll_dice()
y = roll_dice()
add = int(x) + int(y)
if x == '1':
if y == '1':
print(pic1, end = '')
print(pic1)
if y == '2':
print(pic1, end = '')
print(pic2)
if y == '3':
print(pic1, end = '')
print(pic3)
if y == '4':
print(pic1, end = '')
print(pic4)
if y == '5':
print(pic1, end = '')
print(pic5)
if y == '6':
print(pic1, end = '')
print(pic6)
stopper = int(x) + int(y)
if add == (7 or 11):
print(f"You rolled a {add} on your first roll.")
print(f"You win!")
elif add == (2 or 3 or 12):
print(f"You rolled a {add} on your first roll.")
print(f"You lose!")
else:
print(f"You rolled a {add} on your first roll.")
print(f"\nThat's your point. Roll it again before you roll a 7 and lose!")
while add != (7 or stopper):
proceed = input(f"\nPress <Enter> to roll the dice.")
if proceed == (""):
x = roll_dice()
y = roll_dice()
add = int(x) + int(y)
if (add == 7):
print(f"You rolled a 7.")
print(f"You lose!")
break
elif (add == stopper):
print(f"You rolled a {stopper}.")
print(f"You win!")
break
else:
print(f"You rolled a {add}")
main()
我希望输出是两个骰子彼此相邻显示,中间有三个空格,但是实际输出是两个骰子在一行上的非常混乱的扭曲版本。
答案 0 :(得分:2)
您必须将骰子保留为行列表
die3 = [
"+-------+",
"| * |",
"| * |",
"| * |",
"+-------+",
]
die4 = [
"+-------+",
"| * * |",
"| |",
"| * * |",
"+-------+",
]
然后可以使用zip()
创建对[first row of die3, first row of die4]
,[second row of die3, second row of die4]
等。然后可以使用"".join()
连接并显示配对
for rows in zip(die3, die4):
#print(rows)
print(''.join(rows))
+-------++-------+
| * || * * |
| * || |
| * || * * |
+-------++-------+
你可以用更多的骰子做同样的事情
for rows in zip(die3, die4, die4, die3):
print(''.join(rows))
+-------++-------++-------++-------+
| * || * * || * * || * |
| * || || || * |
| * || * * || * * || * |
+-------++-------++-------++-------+
如果在骰子之间需要空格,请在" ".join(rows)
中使用空格
答案 1 :(得分:1)
此行中有一个大问题:
pic1, pic2, pic3, pic4, pic5, pic6 = display_die1(), display_die2(), display_die3(), display_die4(), display_die5(), display_die6()
让我们将其简化为单个任务并发现错误:
pic1 = display_die()
将导致以下结果:
在控制台中:
+-------+
| * * * |
| |
| * * * |
+-------+
还有pic1 == None
。
另一个问题是,您只能逐行打印,而不能跳回。因此,您需要一种机制,以便每个骰子在一行中打印两行。
一种解决方案是将骰子的每一行存储在一个list元素中:
def get_dice_1():
lines = [
"+-------+",
"| * * * |",
"| |",
"| * * * |",
"+-------+"
]
要打印两个骰子:
def print_dices(list_dice_1, list_dice_2):
for i range(len(list_dice_1)):
line = list_dice_1[i] + " " + list_dice_2[i]
print(line)
答案 2 :(得分:1)
我认为最好的解决方案是将骰子存储在列表中,然后可以使用字符串格式化方法打印骰子的每一行。例如:
import random
dice_list = [
[
"+-------+",
"| |",
"| * |",
"| |",
"+-------+"],
[
"+-------+",
"| * |",
"| |",
"| * |",
"+-------+"],
[
"+-------+",
"| * |",
"| * |",
"| * |",
"+-------+"],
[
"+-------+",
"| * * |",
"| |",
"| * * |",
"+-------+"],
[
"+-------+",
"| * * |",
"| * |",
"| * * |",
"+-------+"],
[
"+-------+",
"| * * * |",
"| |",
"| * * * |",
"+-------+"]
]
roll1 = random.randint(0, 5)
roll2 = random.randint(0, 5)
for i in range(5): # number of rows of strings per dice
print("{} {}".format(dice_list[roll1][i], dice_list[roll2][i]))
哪个输出如下:
+-------+ +-------+
| * * * | | * * * |
| | | |
| * * * | | * * * |
+-------+ +-------+
需要注意的是,用这种方法掷骰子的范围是0-5,因此如果您想说print('You rolled a 2 and a 5')
,则必须在roll1
和{{1}上都加1。 },以便数字匹配!
答案 3 :(得分:0)
编辑:
我知道已经为该帖子选择了答案,但这是使用yield关键字的替代解决方案,该解决方案可以按顺序使用。我之前曾有过这样的想法,但是没有足够的时间来编写代码。如果有的话,很高兴看到另一种完成同一件事的方法。 :)
def display_die1():
yield ("+-------+")
yield ("| |")
yield ("| * |")
yield ("| |")
yield ("+-------+")
def display_die2():
yield ("+-------+")
yield ("| * |")
yield ("| |")
yield ("| * |")
yield ("+-------+")
dice1 = display_die1()
dice2 = display_die2()
for line in range(0,5):
print(f"{next(dice1)} {next(dice2)}")
以下是输出:
+-------+ +-------+
| | | * |
| * | | |
| | | * |
+-------+ +-------+
希望对您有帮助!
################我的旧答案使用文档字符串而不是打印每一行将为您提供更一致的输出。
def display_die1():
dice = '''
+-------+
| |
| * |
| |
+-------+
'''
return dice
def display_die2():
dice = '''
+-------+
| * |
| |
| * |
+-------+
'''
return dice
print(f"{str(display_die1())} {str(display_die2())}")
在这种情况下,不是并排,而是一个在另一个上。
这是输出
python ./testing/dice_test.py
+-------+
| |
| * |
| |
+-------+
+-------+
| * |
| |
| * |
+-------+