Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Dice Game error HELP plz (http://www.programmingforums.org/showthread.php?t=10277)

python Jun 10th, 2006 8:47 PM

Dice Game error HELP plz
 
Here is the code:

print "Welcome to RoyalSoftware Main system"
print "Login to use"

usrname = raw_input("What is your username: ")

if usrname == "treymonty":
print "Welcome, " + usrname
psswd = raw_input("Enter your password: ")

if psswd == "password":
print "Welcome to RoyalSoftware"

print """
RoyalSoftware Menu
1 - Game
2 - Directory
3 - Info/Credits
4 - Source Code
"""
choice = raw_input("Choice: ")
if choice == "1":
import random
print "Die Roller"
print "RoyalSoftware"
raw_input("\n\nPress enter to roll")

die1Human = random.randrange(6) + 1
die2Human = random.randrange(6) + 1
die3Human = random.randrange(6) + 1
die4Human = random.randrange(6) + 1
die5Human = random.randrange(6) + 1
die6Human = random.randrange(6) + 1
die1Computer = random.randrange(6) + 1
die2Computer = random.randrange(6) + 1
die3Computer = random.randrange(6) + 1
die4Computer = random.randrange(6) + 1
die5Computer = random.randrange(6) + 1
die6Computer = random.randrange(6) + 1

print usrname + " rolled a", die1Human,"and a", die2Human,"."
print "The computer rolled a", die1Computer,"and a", die2Computer,"."
if die1Human + die2Human > die1Computer + die2Computer:
print "You won!"

if die1Human + die2Human < die1Computer + die2Computer:
print "You lost!"

raw_input("\nPress enter to roll")

print usrname + " rolled a", die3Human,"and a", die4Human,"."
print "The computer rolled a", die3Computer,"and a", die4Computer,"."
if die3Human + die4Human > die3Computer + die4Computer:
print "You won!"

if die3Human + die4Human < die3Computer + die4Computer:
print "You lost!"

raw_input("\nPress enter to roll")

print usrname + " rolled a", die5Human,"and a", die6Human,"."
print "The computer rolled a", die5Computer,"and a", die6Computer,"."
if die5Human + die6Human > die5Computer + die6Computer:
print "You won!"

if die5Human + die6Human < die5Computer + die6Computer:
print "You lost!"

raw_input("\nPress enter to see the final score!")

humanScore = die1Human + die2Human + die3Human + die4Human + die5Human + die6Human
computerScore = die1Computer + die2Computer + die3Computer + die4Computer + die5Computer + die6Computer

print usrname + "'s final score is " + humanScore
print "Computer's final score is " + computerScore

if humanScore > computerScore:
print "You Won!"

if humanScore < computerScore:
print "You Lost!"






if choice == "2":
print "Coding terms Dictoary"


if choice == "3":
print "Credits and Information"
print
"""
RoyalSoftware inc.
We make your software.

RoyalSoftware Core net by'
Trey Montgomery
"""

if choice =="4":
print "it will not be here for a while"

here is the error:
Traceback (most recent call last):
File "C:\Python24\RoyalSoftware", line 73, in -toplevel-
print usrname + "'s final score is " + humanScore
TypeError: cannot concatenate 'str' and 'int' objects

I get this when I am trying to see the final score of the dice game

HELP plz

jaeusm Jun 10th, 2006 8:59 PM

First, use code tags for posting code.

Second, use a format specifier to display the int in a string.
:

print usrname + "'s final score is %d" % humanScore

python Jun 10th, 2006 9:04 PM

Thanks
It worked
I just dont understand how:
:

print usrname + "'s final score is %d" % humanScore
works.
Can someone please explain?
Thanks

jaeusm Jun 10th, 2006 9:20 PM

A format specifier is a placeholder for a value that you will provide. In this case, you provided an integer stored in a variable named 'humanScore'. The % sign outside of the string indicates that after it, all the values to be inserted into the format specifier(s) will be presented.

There are several different types of specifiers. If 'humanScore' was a floating point number instead of an integer, you would use %f as the placeholder in the string. If you wanted to display the hexidecimal representation of a number, you would use the %x specifier.

Specifiers also allow you to format the way a value is displayed. For instance, if you only wanted to display two digits to the right of the decimal point for a floating point value (as you would in the case of dollar amounts), the format specifier would look like this: %.02f
:

print "$%.02f" % 20.123456

output: $20.12


Check Google for a more complete list of specifiers.

titaniumdecoy Jun 11th, 2006 12:30 AM

You could convert the humanScore int to a string using str(x) instead:

:

print usrname + "'s final score is " + str(humanScore)

python Jun 11th, 2006 7:47 AM

Thanks for your help. Hopefully sooner or later i will be helpful.

Cerulean Jun 12th, 2006 12:50 PM

Or you could not bother using %d or other placeholders and just use %s. %s implicitly converts to a string so it won't error out if you decide to change the integer into a list or whatever.
:

print "%s's final score is %s" (usrname, humanScore)
And don't forget code tags in the future ;)


All times are GMT -5. The time now is 11:28 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC