[Tutor] role playing game - help needed
Al Stern
albstern at gmail.com
Wed Dec 8 23:56:44 CET 2010
Peter, Thanks for the advice as to how to use numbers. I had learned this
before but the example in the documentation didn't use 'int' so I thought I
didn't need to when using dictionary values.
Anyway, I finally finished the program. I am sure it isn't the most
efficient code and I suspect I should have used the 'while' loop earlier,
but the bottom line is it seems to work. I will paste what I came up with
in case anyone is interested.
Thanks again to everyone. I couldn't have done it wothout you.
-Al
# character creator / role playing game
# have 30 total points to work with
# set variables
attributes = {"strength": 0, "health": 0, "wisdom": 0, "dexterity": 0}
MAX_POINTS = int(30)
keys = attributes.keys()
values = attributes.values()
list (values)
print("""
Welcome to Quest. The goal of the game is to help our hero achieve his
mission.
He will need strength, health, wisdom and dexterity to survive.
You will have 30 points to 'spend' on these attributes.
Use them wisely. His life depends on it.
"""
)
attributes["strength"] = int(input("\nHow many points do you want to assign
to strength?: "))
attributes["health"] = int(input("\nHow many points do you want to assign to
health?: "))
attributes["wisdom"] = int(input("\nHow many points do you want to assign to
wisdom?: "))
attributes["dexterity"] = int(input("\nHow many points do you want to assign
to dexterity?: "))
#point allocation
point_total = 0
for val in values:
point_total += val
print ("\nThis is how you have chosen to allocate your 30 points.")
print ("\nStrength:",(attributes["strength"]))
print ("Health:", (attributes["health"]))
print ("Wisdom:", (attributes["wisdom"]))
print ("Dexterity:", (attributes["dexterity"]))
available_points = (MAX_POINTS) - (point_total)
while point_total != "":
if point_total > 30:
print ("\nYou have gone over your alloted 30 points.")
print ("Please re-enter your choices. ")
attributes["strength"] = int(input("\nHow many points do you want to
assign to strength?: "))
attributes["health"] = int(input("\nHow many points do you want to
assign to health?: "))
attributes["wisdom"] = int(input("\nHow many points do you want to
assign to wisdom?: "))
attributes["dexterity"] = int(input("\nHow many points do you want to
assign to dexterity?: "))
#point allocation
point_total = 0
for val in values:
point_total += val
print ("\nThis is how you have chosen to allocate your 30 points.")
print ("\nStrength:",(attributes["strength"]))
print ("Health:", (attributes["health"]))
print ("Wisdom:", (attributes["wisdom"]))
print ("Dexterity:", (attributes["dexterity"]))
available_points = (MAX_POINTS) - (point_total)
continue
else:
break
print ("\nYou have", available_points, "points left.")
print ("\nSince you have points left over, you may reallocate your points or
begin your quest.")
choice = int(input("\nTo reallocate, press 1. To begin, press 2: "))
while choice != "":
if choice == 1:
print ("Please re-enter your choices. ")
attributes["strength"] = int(input("\nHow many points do you want to
assign to strength?: "))
attributes["health"] = int(input("\nHow many points do you want to
assign to health?: "))
attributes["wisdom"] = int(input("\nHow many points do you want to
assign to wisdom?: "))
attributes["dexterity"] = int(input("\nHow many points do you want to
assign to dexterity?: "))
#point allocation
point_total = 0
for val in values:
point_total += val
print ("\nThis is how you have chosen to allocate your 30 points.")
print ("\nStrength:",(attributes["strength"]))
print ("Health:", (attributes["health"]))
print ("Wisdom:", (attributes["wisdom"]))
print ("Dexterity:", (attributes["dexterity"]))
available_points = (MAX_POINTS) - (point_total)
print ("\nYou have", available_points, "points left.")
print ("\nSince you have points left over, you may reallocate your
points or begin your quest.")
choice = int(input("\nTo reallocate, press 1. To begin, press 2: "))
elif choice == 2:
break
else:
continue
print ("You are now ready to begin your quest. Good luck.")
input ("\n\nPress the enter key to continue.\n")
On Tue, Dec 7, 2010 at 5:29 PM, Peter Otten <__peter__ at web.de> wrote:
> Al Stern wrote:
>
> > I used the following code and got the following error.
>
> The result of input is always a string.
>
> > attributes["strength"] = input("\nHow many points do you want to assign
> to
> > strength?: ")
>
> Say you type 42 when you run your script. Then the above assignment is
> effectively
>
> attributes["strength"] = "42"
>
> and when you loop over the values you try to add a string to an integer
> which is what Python complains about:
>
> >>> 42 + "42"
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
>
> To get an integer you have to convert the string explicitly:
>
> >>> 42 + int("42")
> 84
>
> The best place to do that is as early as possible, even before you put the
> value into the dictionary:
>
> attributes["strength"] = int(input(...))
>
> (In a real application you'd guard against values that cannot be converted
> to integers)
>
> > #point allocation
> > point_total = 0
> > for val in values:
> > point_total += val
> > print (point_total)
> >
> > and get this error...
> >
> > Traceback (most recent call last):
> > File "C:\Users\Public\Documents\My Python
> programs\role_playing_game1.py",
> > line 26, in <module>
> > point_total += val
> > TypeError: unsupported operand type(s) for +=: 'int' and 'str'
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20101208/f4251069/attachment-0001.html>
More information about the Tutor
mailing list