鱼C论坛

 找回密码
 立即注册
查看: 1258|回复: 5

[已解决]函数,随机数,循环

[复制链接]
发表于 2022-3-26 17:16:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x


Create another function called roll_and_show.
The function should use the throw_dice function to throw the dice.
After that the function should call the show_roll function to show the number you threw as an image.

创建另一个名为 roll_and_show 的函数。
该函数应该使用 throw_dice 函数来掷骰子。
之后,该函数应调用 show_roll 函数以将您投掷的数字显示为图像。


求各位大佬来解!谢谢!
最佳答案
2022-3-26 18:44:12
慕容云蕾 发表于 2022-3-26 17:50
import random
for x in range(1,11):   
    throw_dice = random.randint(1,6)

代码正确很好,只是还差 show_row 和 roll_and_show 函数:
  1. import random

  2. def show_roll(num):
  3.     image = [
  4.         [
  5.             "*****",
  6.             "*   *",
  7.             "* O *",
  8.             "*   *",
  9.             "*****"
  10.         ],
  11.         [
  12.             "*****",
  13.             "*  O*",
  14.             "*   *",
  15.             "*O  *",
  16.             "*****"
  17.         ],
  18.         [
  19.             "*****",
  20.             "*  O*",
  21.             "* O *",
  22.             "*O  *",
  23.             "*****"
  24.         ],
  25.         [
  26.             "*****",
  27.             "*O O*",
  28.             "*   *",
  29.             "*O O*",
  30.             "*****"
  31.         ],
  32.         [
  33.             "*****",
  34.             "*O O*",
  35.             "* O *",
  36.             "*O O*",
  37.             "*****"
  38.         ],
  39.         [
  40.             "*****",
  41.             "*O O*",
  42.             "*O O*",
  43.             "*O O*",
  44.             "*****"
  45.         ]
  46.     ]
  47.     for n in image[num - 1]:
  48.         print(n)
  49.     print()

  50. def throw_dice():
  51.     return random.randint(1,6)

  52. def roll_and_show():
  53.     show_roll(throw_dice())

  54. for i in range(3):
  55.     roll_and_show()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-3-26 17:45:09 | 显示全部楼层
你的代码呢?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-26 17:50:17 | 显示全部楼层

import random
for x in range(1,11):   
    throw_dice = random.randint(1,6)
    print(throw_dice)



小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-26 17:51:49 | 显示全部楼层

#Import libraries
from PIL import Image
import requests
from io import BytesIO
import random


#Dice side image URLs
roll1_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011091.png"
roll2_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011093.png"
roll3_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011095.png"
roll4_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011097.png"
roll5_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011099.png"
roll6_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011104.png"


#Get images from URLs
roll1 = requests.get(roll1_url)
roll2 = requests.get(roll2_url)
roll3 = requests.get(roll3_url)
roll4 = requests.get(roll4_url)
roll5 = requests.get(roll5_url)
roll6 = requests.get(roll6_url)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-3-26 18:00:25 | 显示全部楼层
#Dice side image URLs
roll1_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011091.png"
roll2_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011093.png"
roll3_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011095.png"
roll4_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011097.png"
roll5_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011099.png"
roll6_url = r"https://cdn-icons-png.flaticon.com/128/7011/7011104.png"

#Get images from URLs
roll1 = requests.get(roll1_url)
roll2 = requests.get(roll2_url)
roll3 = requests.get(roll3_url)
roll4 = requests.get(roll4_url)
roll5 = requests.get(roll5_url)
roll6 = requests.get(roll6_url)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-3-26 18:44:12 | 显示全部楼层    本楼为最佳答案   
慕容云蕾 发表于 2022-3-26 17:50
import random
for x in range(1,11):   
    throw_dice = random.randint(1,6)

代码正确很好,只是还差 show_row 和 roll_and_show 函数:
  1. import random

  2. def show_roll(num):
  3.     image = [
  4.         [
  5.             "*****",
  6.             "*   *",
  7.             "* O *",
  8.             "*   *",
  9.             "*****"
  10.         ],
  11.         [
  12.             "*****",
  13.             "*  O*",
  14.             "*   *",
  15.             "*O  *",
  16.             "*****"
  17.         ],
  18.         [
  19.             "*****",
  20.             "*  O*",
  21.             "* O *",
  22.             "*O  *",
  23.             "*****"
  24.         ],
  25.         [
  26.             "*****",
  27.             "*O O*",
  28.             "*   *",
  29.             "*O O*",
  30.             "*****"
  31.         ],
  32.         [
  33.             "*****",
  34.             "*O O*",
  35.             "* O *",
  36.             "*O O*",
  37.             "*****"
  38.         ],
  39.         [
  40.             "*****",
  41.             "*O O*",
  42.             "*O O*",
  43.             "*O O*",
  44.             "*****"
  45.         ]
  46.     ]
  47.     for n in image[num - 1]:
  48.         print(n)
  49.     print()

  50. def throw_dice():
  51.     return random.randint(1,6)

  52. def roll_and_show():
  53.     show_roll(throw_dice())

  54. for i in range(3):
  55.     roll_and_show()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 11:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表