Need help with programming in python for class (beginner level)

F

farhanken

It's for a school assignment. Basically, I need to roll 5 dies with 6 sideseach. So basically, 6 random numbers. That part is easy. Then I need to add it up. Ok, done that. However, I also need to say something along the lines of "your total number was X". That's what I'm having trouble with. I added the dice rolls together and put them into a variable I called "number" but it seems to glitch out that variable is in any command other than "printnumber". Like, if I try to write:

print "<p>your total number was:" number "</p>"

It just doesn't work.

Here is my code so far:


import cgi

form = cgi.FieldStorage()
name = form.getvalue("name")
value = form.getvalue("value")


print """Content-type: text/html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head>
<title>A CGI Script</title>
</head><body>
"""
import random

die1 = random.randint(1,6)
die2 = random.randint(1,6)
die3 = random.randint(1,6)
die4 = random.randint(1,6)
die5 = random.randint(1,6)
print die1, die2, die3, die4, die5
number = die1 + die2 + die3 + die4 + die5
print "<p>The total rolled was: "number" </p>"

print "<p>Thanks for playing, " + name + ".</p>"
print "<p>You bet the total would be at least " + value + ".</p>"
print "</body></html>"
 
J

Johannes Findeisen

On Fri, 29 Nov 2013 16:31:21 -0800 (PST)
print "<p>The total rolled was: "number" </p>"

The above line is wrong. You did it right below:
print "<p>Thanks for playing, " + name + ".</p>"
print "<p>You bet the total would be at least " + value + ".</p>"

Do this:

print "<p>The total rolled was: " + number + " </p>"

Regards,
Johannes
 
J

Johannes Findeisen

On Fri, 29 Nov 2013 16:31:21 -0800 (PST)


The above line is wrong. You did it right below:


Do this:

print "<p>The total rolled was: " + number + " </p>"

Sorry, that was wrong! You need to convert to a string when
concatenating...

print "<p>The total rolled was: " + str(number) + " </p>"
 
T

Tim Chase

It's for a school assignment.

Thanks for the honesty--you'll get far more helpful & useful replies
because of that. :)
put them into a variable I called "number" but it seems to glitch
out that variable is in any command other than "print number".
Like, if I try to write:

print "<p>your total number was:" number "</p>"

It just doesn't work.

You're so close. Python doesn't let you directly combine strings and
numbers like that, especially without any operator. However it does
offer several ways to combine strings:

print "convert to string and use " + str(number) + " a '+' operator"
print "use %s classic C-style formatting" % number
print "use new {} formatting style".format(number)

-tkc
 
M

Mark Lawrence

Sorry, that was wrong! You need to convert to a string when
concatenating...

print "<p>The total rolled was: " + str(number) + " </p>"

Wrong again, or at least overengineered.

print "<p>The total rolled was:", number, </p>"

You don't even need the spaces as print kindly does it for you :)
 
T

Tim Chase

Wrong again, or at least overengineered.

print "<p>The total rolled was:", number, </p>" ^

You don't even need the spaces as print kindly does it for you :)

but you could at least include the missing quotation mark ;-)

-tkc
 
J

Johannes Findeisen

:)

It's way past my bedtime :)

Same to me... I should get more sleep before answering.

Anyway, Thank you for the explanation of this.

Sleep well... ;)

Johannes
 
J

Julio Schwarzbeck

It's for a school assignment. Basically, I need to roll 5 dies with 6 sides each. So basically, 6 random numbers. That part is easy. Then I need to add it up. Ok, done that. However, I also need to say something along the lines of "your total number was X". That's what I'm having trouble with. I added the dice rolls together and put them into a variable I called "number" but it seems to glitch out that variable is in any command other than "print number". Like, if I try to write:

print "<p>your total number was:" number "</p>"

It just doesn't work.

Here is my code so far:


import cgi

form = cgi.FieldStorage()
name = form.getvalue("name")
value = form.getvalue("value")


print """Content-type: text/html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head>
<title>A CGI Script</title>
</head><body>
"""
import random

die1 = random.randint(1,6)
die2 = random.randint(1,6)
die3 = random.randint(1,6)
die4 = random.randint(1,6)
die5 = random.randint(1,6)
print die1, die2, die3, die4, die5
number = die1 + die2 + die3 + die4 + die5
print "<p>The total rolled was: "number" </p>"

print "<p>Thanks for playing, " + name + ".</p>"
print "<p>You bet the total would be at least " + value + ".</p>"
print "</body></html>"

My two "favorites":

print '<p>The total rolled was: %s </p>' % number
[py 2.7+] print('<p>The total rolled was: {} </p>'.format(number))

Cheers,

-- Speedbird
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top