Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 10th, 2006, 9:47 PM   #1
python
Newbie
 
Join Date: Jun 2006
Posts: 7
Rep Power: 0 python is on a distinguished road
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
python is offline   Reply With Quote
Old Jun 10th, 2006, 9:59 PM   #2
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
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
jaeusm is offline   Reply With Quote
Old Jun 10th, 2006, 10:04 PM   #3
python
Newbie
 
Join Date: Jun 2006
Posts: 7
Rep Power: 0 python is on a distinguished road
Thanks
It worked
I just dont understand how:
print usrname + "'s final score is %d" % humanScore
works.
Can someone please explain?
Thanks
python is offline   Reply With Quote
Old Jun 10th, 2006, 10:20 PM   #4
jaeusm
Programmer
 
jaeusm's Avatar
 
Join Date: Feb 2006
Location: Columbus, OH
Posts: 84
Rep Power: 3 jaeusm is on a distinguished road
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.
jaeusm is offline   Reply With Quote
Old Jun 11th, 2006, 1:30 AM   #5
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 930
Rep Power: 4 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
You could convert the humanScore int to a string using str(x) instead:

print usrname + "'s final score is " + str(humanScore)
titaniumdecoy is online now   Reply With Quote
Old Jun 11th, 2006, 8:47 AM   #6
python
Newbie
 
Join Date: Jun 2006
Posts: 7
Rep Power: 0 python is on a distinguished road
Thanks for your help. Hopefully sooner or later i will be helpful.

Last edited by python; Jun 11th, 2006 at 9:01 AM.
python is offline   Reply With Quote
Old Jun 12th, 2006, 1:50 PM   #7
Cerulean
Professional Programmer
 
Cerulean's Avatar
 
Join Date: Apr 2005
Location: London, England
Posts: 459
Rep Power: 4 Cerulean is on a distinguished road
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
Cerulean is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:14 AM.

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