Multiversal_Coder_101
Multiversal_Coder_101

Reputation: 23

How do I get my code to increase per time it's repeated

# Imports
import random
import time as t

# Lists
D=[1,2,3,4,5,6]

# Varibles
dr=0
total=0

# Defines the amount of total of the dice
def rp(num):
    for r in range(num):
        dr+1
        DR=random.choice(D)
        total=+1
        print('Die', dr, 'is', DR ,end = '. ')

How do I get dr, to increase every time this code runs. I've tryed everything, also, don't blame me if this looks like a repost. It's my second time using this.

for r in range(num):
        dr+1
        DR=random.choice(D)
        total=+1
        print('Die', dr, 'is', DR ,end = '. ')

Upvotes: 0

Views: 43

Answers (2)

0xApollyon
0xApollyon

Reputation: 80

By what the OP says , I assume he/she wants the variable dr to increase everytime the code in run

That can be accomplished by storing the value of dr in an external file and increasing it everytime to program is run

import os
import random
import time as t

dr_file = "/path/to/file"
dr_file = open(d_file , "r")
dr = dr_file.read()
dr_file.close()

dr = int(dr) #Because it will be a string by default
#then write something to increase the value of dr , like dr = dr + something

D=[1,2,3,4,5,6]
total=0

def rp(num):
    for r in range(num):
        dr+1
        DR=random.choice(D)
        total=+1
        print('Die', dr, 'is', DR ,end = '. ')

Hope this helps :)

Upvotes: 1

NGY
NGY

Reputation: 96

I assume you want your program to generate num die throws each time you run it. Your code looks quite immature(not a discouragement) so I assume you are still learning. Kudos.

You can use this:

import random

def throwDies(num):
    for c in range(num):
        throw = random.randint(1,6) #gets a random number between 1 and 6 inclusive
        print("Throw number %d  is #%d"%(c,throw))

x = int(input("Enter the number of throws to make: "))
throwDies(x)

Upvotes: 2

Related Questions