慕容云蕾 发表于 2022-3-26 17:16:20

函数,随机数,循环



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 17:45:09

你的代码呢?

慕容云蕾 发表于 2022-3-26 17:50:17

傻眼貓咪 发表于 2022-3-26 17:45
你的代码呢?

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



慕容云蕾 发表于 2022-3-26 17:51:49

傻眼貓咪 发表于 2022-3-26 17:45
你的代码呢?

#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)

慕容云蕾 发表于 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)

傻眼貓咪 发表于 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 函数:
import random

def show_roll(num):
    image = [
      [
            "*****",
            "*   *",
            "* O *",
            "*   *",
            "*****"
      ],
      [
            "*****",
            "*O*",
            "*   *",
            "*O*",
            "*****"
      ],
      [
            "*****",
            "*O*",
            "* O *",
            "*O*",
            "*****"
      ],
      [
            "*****",
            "*O O*",
            "*   *",
            "*O O*",
            "*****"
      ],
      [
            "*****",
            "*O O*",
            "* O *",
            "*O O*",
            "*****"
      ],
      [
            "*****",
            "*O O*",
            "*O O*",
            "*O O*",
            "*****"
      ]
    ]
    for n in image:
      print(n)
    print()

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

def roll_and_show():
    show_roll(throw_dice())

for i in range(3):
    roll_and_show()
页: [1]
查看完整版本: 函数,随机数,循环