حالت آفلاین — محتوا از حافظهٔ کش نمایش داده می‌شود
همهٔ تمرین‌ها

تاس

تمرین آسان 3884 visibility link download flag

برنامه ای بنویسید که همانند یک تاس، با هر بار اجرا، یک عدد تصادفی بین 1 تا 6 در خروجی چاپ کند

پاسخ‌ها 19

import random
from os import system
while True:
    system('cls')
    random_choice=random.randint(1,6)
    user_game=input("Press and for Exit enetr q : ")
    if user_game == 'q':
        break
    else:
        if random_choice == 1:
            print("[       ]")
            print("[   0   ]")
            print("[       ]")
        elif random_choice == 2:
            print("[   0   ]")
            print("[       ]")
            print("[   0   ]")
        elif random_choice == 3:
            print("[   0   ]")
            print("[   0   ]")
            print("[   0   ]")
        elif random_choice == 4:
            print("[0     0]")
            print("[       ]")
            print("[0     0]")
        elif random_choice == 5:
            print("[0     0]")
            print("[   0   ]")
            print("[0     0]")
        elif random_choice == 6:
            print("[0     0]")
            print("[0     0]")
            print("[0     0]")
import random

def roll_dice():
    # انتخاب یک عدد تصادفی بین 1 تا 6
    dice_number = random.randint(1, 6)
    return dice_number

# اجرای تابع و چاپ نتیجه
result = roll_dice()
print("تاس رول شده:", result)

من به دو دلیل از ژنراتور استفاده کردم.
1. گفته شده که با هر بار اجرا یک عدد چاپ کند.
2. استفاده از ژنراتور بهینه تر است در این مثال.

from random import randint
def dice():
    yield randint(1, 6)
dice_gen=dice()
print(next(dice_gen))
public static void main(String[] args) {
        int RolDice = (int) (Math.random() * 6) + 1;
        if (RolDice == 1) {
            System.out.println("[      ]");
            System.out.println("[   0  ]");
            System.out.println("[      ]");
        }else if (RolDice == 2) {
            System.out.println("[0     ]");
            System.out.println("[      ]");
            System.out.println("[     0]");
        }else if (RolDice == 3) {
            System.out.println("[0      ]");
            System.out.println("[   0   ]");
            System.out.println("[      0]");
        }else if (RolDice == 4) {
            System.out.println("[0    0]");
            System.out.println("[      ]");
            System.out.println("[0    0]");
        }else if (RolDice == 5) {
            System.out.println("[0    0]");
            System.out.println("[   0  ]");
            System.out.println("[0    0]");
        }else if (RolDice == 6) {
            System.out.println("[0    0]");
            System.out.println("[0    0]");
            System.out.println("[0    0]");
        }
    }

ارسال جواب

جوابت را با Markdown بنویس و ثبت کن

×
بستن