python - 您如何显示具有多行不同功能的输出以彼此相邻打印?
问题描述
基本上,我的任务是模拟掷骰子并根据掷出的骰子并排打印出来。每个骰子占用 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()
我希望输出是两个骰子并排显示,中间有三个空格,但实际输出是两个骰子在一条线上的非常混乱、扭曲的版本。
解决方案
您必须将骰子保留为行列表
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)
推荐阅读
- javascript - 尝试在 jQuery 中向图像添加类时遇到问题
- css - 如何在 react-native 应用程序中剪辑边框?
- java - RSA 我应该使用 X.509 还是 PKCS #1
- python - str.translate() 替换空白字符
- java - 以下 Java 程序的意外行为
- c# - System.IO.Exception 与 UWP FulltrustLauncher EXE
- laravel - 方法 Illuminate\Database\Eloquent\Collection::orWhere 不存在
- javascript - 使用 chai js 进行自动化测试
- entity-framework - 使用asp.net核心中的连接查询在实体框架中获取用户角色
- ios - Swift 4.2 - 如何从 tableView 中的“didSelectRowAtIndexPath”方法调用“indexPath.row”到另一个类?