When i add a randInt to my gp(Gold peices)...

This is the place for queries that don't fit in any of the other categories.

When i add a randInt to my gp(Gold peices)...

Postby Im Anonymous » Wed Jun 12, 2013 10:55 am

When i add a randInt to my gp(Gold pieces) it doesn't save, for example i would generate a randInt and say ("you found "+randint+" Gold pieces). How can i actually make this random number add to my existing Gold pieces and make it save so when i generate a new random number it will add the new random number to my gold peices? Here is my code

Code: Select all
from random import randint

enter = ('')

def Enter():
    raw_input("")
   
class Me:
    def __init__(self, lvl, gp):
        self.lvl   = lvl
        self.gp    = gp

I = Me(1, 25)
foundGp = randint(1,75)
myGp = (foundGp+I.gp)

print ("Please press 'Enter' to continue...")
Enter()
print ("You have just found "+str(foundGp)+" gold peices!")
print ("You now have "+str(myGp)+" Gold in your bag.")
Enter()
print ("You are Level: "+str(I.lvl)+".")
Enter()
Im Anonymous
 
Posts: 9
Joined: Fri Jun 07, 2013 1:29 am

Re: When i add a randInt to my gp(Gold peices)...

Postby metulburr » Wed Jun 12, 2013 11:46 am

If your using classes, you might as well just use the class to save it instead of a global variable

Code: Select all
import random

class Character:
    def __init__(self, gold):
        self.gold = gold

player = Character(10)

for i in range(3):
    print('players gold: {}'.format(player.gold))
    num = random.randint(1, 75)
    player.gold += num
    print('{} was added to players gold'.format(num))
print('players gold: {}'.format(player.gold))
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1500
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: When i add a randInt to my gp(Gold peices)...

Postby Im Anonymous » Wed Jun 12, 2013 12:35 pm

Thanks i used what you taught me and implemented it in to mine, but now when i reach the bit of the code where it tells me my level the whole process repeats itself again for 3 times and then i get a error. So i added a break to the end of the code and it stops this but now i can't call playerGp or foundGp :S

Code: Select all
class Me:
    def __init__(self, lvl, gp):
        self.lvl   = lvl
        self.gp    = gp

Player = Me(1, 25)

for i in range(3):
    print userName+(" gold: {}".format(Player.gp))
    enterToContinue()
    foundGp = randint(1,75)
    Player.gp += foundGp
    print ("You have just found '{}' gold pieces,".format(foundGp))
    print ("you now have {} gold in your bag.".format(Player.gp))
    enterToContinue()
    print userName+(" level: "+str(Player.lvl)+".")
    break
Im Anonymous
 
Posts: 9
Joined: Fri Jun 07, 2013 1:29 am

Re: When i add a randInt to my gp(Gold peices)...

Postby Im Anonymous » Wed Jun 12, 2013 12:46 pm

Never mind i totally understand now, thanks.
Im Anonymous
 
Posts: 9
Joined: Fri Jun 07, 2013 1:29 am


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 3 guests