Jump to content

New value being saved from old?

- - - - -

  • Please log in to reply
13 replies to this topic

#1
synk

synk

    Newbie

  • Members
  • PipPip
  • 18 posts
What i'm trying to ask is, how can i make a new value continue to save on from a old value.

For example:

I have:


max_points = 30


and i have a dict:

attri = {"strength": 0,"health": 0,"wisdom": 0,"dexterity":0}


Now i have a while loop and it asks a user a question:


add = int(raw_input("How many points would you like to add? "))


Now if i do this:

max_points -= attri ["strength"]

It works fine and the max_points goes down as supposed to, but it doesnt save.

Like here:


attri ["health"] = add

max_points -= attri["health"]


Now i need the previous max_points to save over onto when i subtract max_points again, but it resets to it's normal value (30).

full code:


#role playing attributes

#add = int(raw_input("How many points would you like to add? "))

attri = {"strength": 0,"health": 0,"wisdom": 0,"dexterity":0}

choice = None

max_points = 30

#str_points = count - points [0]


while choice != "4":


    print \

     """

         1. Spend your points

         2. Check how many points you have

         3. Remove points

         4. Exit the game

         """


    choice = raw_input("Choice? ")

    if choice == "4":

        print "Thanks for playing"

    elif choice == "1":

        quest = raw_input("Which attribute would you like to add points to? ")

        if quest == "strength":

            add = int(raw_input("How many points would you like to add? "))

            attri ["strength"] = add

            max_points -= attri ["strength"]

            print "You have added ", add, "points to strength and have ", max_points, "remaining."

        if quest == "health":

            add = int(raw_input("How many points would you like to add? "))

            attri ["health"] = add

            print max_points

            print "You have added ", add, "points to strength and have ", max_points, "remaining."

        


raw_input("x")

    



#2
restored

restored

    Newbie

  • Members
  • PipPip
  • 14 posts
looks normally

[guest@localhost tmp]$ python t.py


         1. Spend your points

         2. Check how many points you have

         3. Remove points

         4. Exit the game

         

Choice? 1

Which attribute would you like to add points to? strength

How many points would you like to add? 10

You have added  10 points to strength and have  20 remaining.


         1. Spend your points

         2. Check how many points you have

         3. Remove points

         4. Exit the game

         

Choice? 1

Which attribute would you like to add points to? strength

How many points would you like to add? 25

You have added  25 points to strength and have  -5 remaining.


         1. Spend your points

         2. Check how many points you have

         3. Remove points

         4. Exit the game

         

Choice? 4

Thanks for playing

x

[guest@localhost tmp]$



#3
synk

synk

    Newbie

  • Members
  • PipPip
  • 18 posts
Thats weird, it wasn't working yesterday!

How can i add a limit to the points? Like so it doesn't go negative.

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
  • Location:::1
You can check if it's 0 or lower, and then assign new value.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
synk

synk

    Newbie

  • Members
  • PipPip
  • 18 posts
Thank you, works great.

One more question:

How can i add the key values from more than one dict? For example
If key "strength" had a value of 10 and key "health" had a value of 20, how could i use them in a simple maths equation.

I've tryed:

max_points -= attri ["strength", "health"]

But i get a error, any ideas?


Also,

If i wanted to delete a value of a key how would that be possible?
Say if a user entered 5 as a number and wanted to remove it from key "strength" how would i write that?


Thanks again.

#6
restored

restored

    Newbie

  • Members
  • PipPip
  • 14 posts

synk said:

If i wanted to delete a value of a key how would that be possible?
d['key'] = None or del d['key']

synk said:

If key "strength" had a value of 10 and key "health" had a value of 20, how could i use them in a simple maths equation.
s = d['strength'] + d['health'] or d['str_hea_sum'] = d['strength'] + d['health']

#7
synk

synk

    Newbie

  • Members
  • PipPip
  • 18 posts
Thats great, thanks.

But i mean the value of the key, with d["key"] = None or del d["key"] the key gets removed, along with the value. Is there a function that can just delete the value?

None the less, helped alot.

#8
synk

synk

    Newbie

  • Members
  • PipPip
  • 18 posts
son_father = {"x":"x1", "y":"y1", "z":"z1"}

x value is x1, is there a way i can add another value to to it? What i mean by this is, i have a challange to make a program which pairs up sons and fathers, and grandfathers, saying son is x and father is x1, how can i add another value to make it grandfather? The rule is, i have to only use one dictionairy.

#9
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 890 posts
  • Location:::1
Make lists if you can (= are allowed to).

d = {'a': ['1', '2', '3'], 'b': ['4', '5', '6']}


A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#10
synk

synk

    Newbie

  • Members
  • PipPip
  • 18 posts
Thanks, thats great, can call it out with indexing or slicing also.

#11
restored

restored

    Newbie

  • Members
  • PipPip
  • 14 posts

synk said:

Is there a function that can just delete the value?
you cannot have a dictionary key without the value, dictionaries consist of pairs key: value, so to remove the value you have to do d['key'] = None

to assign another value to a key you do

son_father = {"x":"x1", "y":"y1", "z":"z1"}

son_father['x'] = 'x2'


you can do also

son_father = {"x":"x1", "y":"y1", "z":"z1"}

son_father['x'] = 'x2' + 'is a new grandfather'

print(son_father['x'])


Edited by restored, 20 February 2011 - 11:31 PM.


#12
synk

synk

    Newbie

  • Members
  • PipPip
  • 18 posts
Thanks, sorry to bump this up again (didn't want to make a new thread.)

But why is this not working?


#Tic Tac Toe Lamine


board = [0,1,2,

         3,4,5,

         6,7,8]


def board():

    print "\t",board[0], "|", board[1], "|", board[2]

    print "\t---------"

    print "\t",board[3], "|", board[4], "|", board[5]

    print "\t---------"

    print "\t",board[6], "|", board[7], "|", board[8]

    

def main ():

    while True:

        player = int(raw_input("What move? "))

        if board[player] != "x" and board [player] != "o":

            board[player] = "x"

        else:

            print "that spot is taken"


        board()



main()


I get the error:


>>> 

What move? 2


Traceback (most recent call last):

  File "C:\Users\Lamine\Desktop\Python\tictactoe2.py", line 25, in <module>

    main()

  File "C:\Users\Lamine\Desktop\Python\tictactoe2.py", line 17, in main

    if board[player] != "x" and board [player] != "o":

TypeError: 'function' object is not subscriptable






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users