Ahmad Syahmi
Ahmad Syahmi

Reputation: 29

How can I make a number randomize like a countdown timer in Python?

I'm just trying to make a simple, beginner dice project in Python. And I'm trying to step it up a bit by displaying random number kinda like a countdown timer in one line in terminal. For example like a dice in the link below but in numbers.

The link: https://rolladie.net/

The code:

import random

dice = ['1','2','3','4','5','6']

throwing = True
while throwing:
    throw_dice = random.choice(dice)

    if throw_dice == '1':
        print("You landed on 1!")
    if throw_dice == '2':
        print("You landed on 2!")
    if throw_dice == '3':
        print("You landed on 3!")
    if throw_dice == '4':
        print("You landed on 4!")
    if throw_dice == '5':
        print("You landed on 5!")
    if throw_dice == '6':
        print("You landed on 6!")

    throw_again = input("Want to throw again?(y/n)\n")
    if throw_again == 'y':
        throwing = True
    else:
        quit()

Is it possible to do it? Thanks in advance to anyone answering my question. Sorry if my question kinda weird and hard to understand.

Upvotes: 2

Views: 581

Answers (3)

Krish
Krish

Reputation: 55

I'm not sure what you mean by a countdown timer but to display a random number you can use the random module (which you are using) to display a random number, between a range if you wish too.

print(random.randint(0,6))

This will print a random number between the range of 0 to 6. It is inclusive so 0 and 6 can be picked as well. The code uses the random module and chooses an interger hence the ranINT between the given range. You can also use this link to learn more about the random module: https://www.tutorialsteacher.com/python/random-module.

Upvotes: 0

Faraaz Kurawle
Faraaz Kurawle

Reputation: 1148

from random import randint
import time

for i in range(100):
    a=randint(1,6)
    time.sleep(0.2)
    print(a,end="\r")

Here i have used random.randint function to pick random number and used end='\r' inside print function so that the line itself gets updated.

And by seeing your code, here's what you can do make it more efficient:

import random
import time
while 1:
    throw_dice = random.randint(1,6)
    for i in range(5):
        a=random.randint(1,6)
        time.sleep(0.2)
        print(a,end="\r")

    print(f"You landed on {throw_dice}!")

    throw_again = input("Want to throw again?(y/n)\n")
    if throw_again == 'n':
        quit()

This code works exactly same as yours, but less line required. I have used string formatting or f string in the print function to print string as well as the variable value. You can also use print("You landed on"+throw_dice+"!") instead of formatting, but using formatting is a good practice

Upvotes: 1

balu
balu

Reputation: 1143

Could not understand what you meant. But going by the link, you could do something like this

import random
import time

throwing = True
while throwing:
    dice_faces = int(input('Choose no of faces of the dice '))
    
    print('Dice is rolling ...')
    
    for i in range(10):
        throw_dice = random.choice(range(1,dice_faces+1))
        print(f'{throw_dice} \r', end="")
        time.sleep(0.4)

    print(f'You landed on {throw_dice}')

    throw_again = input("Want to throw again?(y/n)\n").lower()
    throwing = True if throw_again == 'y' else False
        
        

Upvotes: 0

Related Questions