希望能够用比较基础的python语言完成,大学生的课程,要有完整的代码截图和语言。
如何模拟用户登录,设计剪刀石头布游戏,模拟掷骰子?(语言-python)
希望能够用比较基础的python语言完成,大学生的课程,要有完整的代码截图和语言。
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- Ko-walski 2022-10-27 22:38关注
骰子部分:
import random DICE_ART = { 1: ( "┌─────────┐", "│ │", "│ ● │", "│ │", "└─────────┘", ), 2: ( "┌─────────┐", "│ ● │", "│ │", "│ ● │", "└─────────┘", ), 3: ( "┌─────────┐", "│ ● │", "│ ● │", "│ ● │", "└─────────┘", ), 4: ( "┌─────────┐", "│ ● ● │", "│ │", "│ ● ● │", "└─────────┘", ), 5: ( "┌─────────┐", "│ ● ● │", "│ ● │", "│ ● ● │", "└─────────┘", ), 6: ( "┌─────────┐", "│ ● ● │", "│ ● ● │", "│ ● ● │", "└─────────┘", ), } DIE_HEIGHT = len(DICE_ART[1]) DIE_WIDTH = len(DICE_ART[1][0]) DIE_FACE_SEPARATOR = " " def parse_input(input_string): if input_string.strip() in {"1", "2", "3", "4", "5", "6"}: return int(input_string) else: print("Please enter a number from 1 to 6.") raise SystemExit(1) def roll_dice(num_dice): roll_results = [] for _ in range(num_dice): roll = random.randint(1, 6) roll_results.append(roll) return roll_results def generate_dice_faces_diagram(dice_values): dice_faces = _get_dice_faces(dice_values) dice_faces_rows = _generate_dice_faces_rows(dice_faces) width = len(dice_faces_rows[0]) diagram_header = " RESULTS ".center(width, "~") dice_faces_diagram = "\n".join([diagram_header] + dice_faces_rows) return dice_faces_diagram def _get_dice_faces(dice_values): dice_faces = [] for value in dice_values: dice_faces.append(DICE_ART[value]) return dice_faces def _generate_dice_faces_rows(dice_faces): dice_faces_rows = [] for row_idx in range(DIE_HEIGHT): row_components = [] for die in dice_faces: row_components.append(die[row_idx]) row_string = DIE_FACE_SEPARATOR.join(row_components) dice_faces_rows.append(row_string) return dice_faces_rows num_dice_input = input("你想掷多少个骰子? [1-6] ") num_dice = parse_input(num_dice_input) roll_results = roll_dice(num_dice) dice_face_diagram = generate_dice_faces_diagram(roll_results) print(f"\n{dice_face_diagram}")
其他由网友实现吧太麻烦🫠
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 stm32开发clion时遇到的编译问题
- ¥15 lna设计 源简并电感型共源放大器
- ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
- ¥15 Vue3地图和异步函数使用
- ¥15 C++ yoloV5改写遇到的问题
- ¥20 win11修改中文用户名路径
- ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
- ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
- ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
- ¥15 帮我写一个c++工程