سورس شبیه سازی تاس (Dice Roll Simulator) با پایتون

سورس شبیه سازی تاس (Dice Roll Simulator) با پایتون

سورس شبیه سازی تاس (Dice Roll Simulator) با پایتون. برای شبیه سازی تاس انداختن با پایتون، از ماژول تصادفی در پایتون استفاده می کنم. ماژول تصادفی را می توان به راحتی به کد شما وارد کرد زیرا از قبل در زبان برنامه نویسی پایتون انجام شده است.

 

سورس شبیه سازی تاس با پایتون

پس از وارد کردن ماژول تصادفی، به تمام عملکردهای موجود در ماژول دسترسی دارید. این یک لیست بسیار طولانی است، اما برای اهداف ما، از تابع random.randint() استفاده می کنیم. این تابع یک عدد صحیح تصادفی را بر اساس شروع و پایانی که ما مشخص می کنیم برمی گرداند. کوچکترین مقدار یک تاس انداختن 1 و بزرگترین آن 6 است، از این منطق می توان برای شبیه سازی یک تاس استفاده کرد. این مقدار شروع و پایان را به ما می دهد تا در تابع random.randint() از آن استفاده کنیم. حالا بیایید ببینیم که چگونه یک تاس انداختن را با پایتون شبیه سازی کنیم:

#importing module for random number generation
import random

#range of the values of a dice
min_val = 1
max_val = 6

#to loop the rolling through user input
roll_again = "yes"

#loop
while roll_again == "yes" or roll_again == "y":
    print("Rolling The Dices...")
    print("The Values are :")
    
    #generating and printing 1st random integer from 1 to 6
    print(random.randint(min_val, max_val))
    
    #generating and printing 2nd random integer from 1 to 6
    print(random.randint(min_val, max_val))
    
    #asking user to roll the dice again. Any input other than yes or y will terminate the loop
    roll_again = input("Roll the Dices Again?")

خروجی به شکل زیر است

Rolling The Dices…
The Values are :
5
4
Roll the Dices Again?yes
Rolling The Dices…
The Values are :
1
3

To simulate a dice roll with Python, I’ll be using the random module in Python. The random module can be imported easily into your code as it is preinstalled in the Python programming language.

After importing the random module, you have access to all the functions included in the module. It’s a pretty long list, but for our purposes, we’ll use the random.randint() function. This function returns a random integer based on the start and end we specify.

The smallest value of a dice roll is 1 and the largest is 6, this logic can be used to simulate a dice roll. This gives us the start and end values to use in our random.randint() function. Now let’s see how to simulate a dice roll with Python:

 

مطالب مرتبط و مفید :

  1. سورس اختصاری کردن کلمات در پایتون
  2. سورس Alarm Clock با پایتون
  3. سورس ساخت داستان تصادفی در پایتون
  4. سورس ساخت پسورد تصادفی در پایتون
  5. سورس بازی سنگ کاغذ قیچی در پایتون
4.8/5 - (9 امتیاز)

راستی! برای دریافت مطالب جدید در کانال تلگرام یا پیج اینستاگرام سورس باران عضو شوید.

آموزش جامع برنامه نویسی PHP و MySQL به همراه صفر تا صد ساخت فروشگاه اینترنتی
  • انتشار: ۲۱ فروردین ۱۴۰۲

دسته بندی موضوعات

آخرین محصولات فروشگاه

مشاهده همه

نظرات

بازخوردهای خود را برای ما ارسال کنید

این سایت از اکیسمت برای کاهش هرزنامه استفاده می کند. بیاموزید که چگونه اطلاعات دیدگاه های شما پردازش می‌شوند.