Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #60779 > unrolled thread
| Started by | farhanken@gmail.com |
|---|---|
| First post | 2013-11-29 16:31 -0800 |
| Last post | 2013-12-01 17:01 -0800 |
| Articles | 10 — 6 participants |
Back to article view | Back to comp.lang.python
Need help with programming in python for class (beginner level) farhanken@gmail.com - 2013-11-29 16:31 -0800
Re: Need help with programming in python for class (beginner level) Johannes Findeisen <mailman@hanez.org> - 2013-11-30 01:38 +0100
Re: Need help with programming in python for class (beginner level) Eduardo A. Bustamante López <dualbus@gmail.com> - 2013-11-29 16:45 -0800
Re: Need help with programming in python for class (beginner level) Johannes Findeisen <mailman@hanez.org> - 2013-11-30 01:49 +0100
Re: Need help with programming in python for class (beginner level) Tim Chase <python.list@tim.thechases.com> - 2013-11-29 18:53 -0600
Re: Need help with programming in python for class (beginner level) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-30 00:59 +0000
Re: Need help with programming in python for class (beginner level) Tim Chase <python.list@tim.thechases.com> - 2013-11-29 19:06 -0600
Re: Need help with programming in python for class (beginner level) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-30 01:08 +0000
Re: Need help with programming in python for class (beginner level) Johannes Findeisen <mailman@hanez.org> - 2013-11-30 02:16 +0100
Re: Need help with programming in python for class (beginner level) Julio Schwarzbeck <julio@techfuel.net> - 2013-12-01 17:01 -0800
| From | farhanken@gmail.com |
|---|---|
| Date | 2013-11-29 16:31 -0800 |
| Subject | Need help with programming in python for class (beginner level) |
| Message-ID | <e9d18514-1da5-471a-bf39-5f67d3d8fef7@googlegroups.com> |
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>"
[toc] | [next] | [standalone]
| From | Johannes Findeisen <mailman@hanez.org> |
|---|---|
| Date | 2013-11-30 01:38 +0100 |
| Message-ID | <mailman.3408.1385772023.18130.python-list@python.org> |
| In reply to | #60779 |
On Fri, 29 Nov 2013 16:31:21 -0800 (PST) farhanken@gmail.com wrote: > 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
[toc] | [prev] | [next] | [standalone]
| From | Eduardo A. Bustamante López <dualbus@gmail.com> |
|---|---|
| Date | 2013-11-29 16:45 -0800 |
| Message-ID | <mailman.3409.1385772355.18130.python-list@python.org> |
| In reply to | #60779 |
On Fri, Nov 29, 2013 at 04:31:21PM -0800, farhanken@gmail.com wrote: > 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. That would be: print "<p>your total number was" + str(number) + "</p>" Notice two differences: - Used the + operator to concatenate two strings: "foo" + "bar" == "foobar" - Converted the number from integer to string, you can't do: <int> + <str> directly, you have to either int(<str>) + <int> if you want to do an integer addition, or: str(<int>) + <str>, if you want string concatenation. You didn't use an operator, and «"string" variable "string"» is not valid python. -- Eduardo Alan Bustamante López
[toc] | [prev] | [next] | [standalone]
| From | Johannes Findeisen <mailman@hanez.org> |
|---|---|
| Date | 2013-11-30 01:49 +0100 |
| Message-ID | <mailman.3410.1385772643.18130.python-list@python.org> |
| In reply to | #60779 |
On Sat, 30 Nov 2013 01:38:36 +0100 Johannes Findeisen wrote: > On Fri, 29 Nov 2013 16:31:21 -0800 (PST) > farhanken@gmail.com wrote: > > > 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>" Sorry, that was wrong! You need to convert to a string when concatenating... print "<p>The total rolled was: " + str(number) + " </p>"
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-11-29 18:53 -0600 |
| Message-ID | <mailman.3411.1385772707.18130.python-list@python.org> |
| In reply to | #60779 |
On 2013-11-29 16:31, farhanken@gmail.com wrote:
> 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
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-11-30 00:59 +0000 |
| Message-ID | <mailman.3412.1385773176.18130.python-list@python.org> |
| In reply to | #60779 |
On 30/11/2013 00:49, Johannes Findeisen wrote: > On Sat, 30 Nov 2013 01:38:36 +0100 > Johannes Findeisen wrote: > >> On Fri, 29 Nov 2013 16:31:21 -0800 (PST) >> farhanken@gmail.com wrote: >> >>> 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>" > > 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 :) -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2013-11-29 19:06 -0600 |
| Message-ID | <mailman.3413.1385773527.18130.python-list@python.org> |
| In reply to | #60779 |
On 2013-11-30 00:59, Mark Lawrence wrote:
> 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
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-11-30 01:08 +0000 |
| Message-ID | <mailman.3415.1385773806.18130.python-list@python.org> |
| In reply to | #60779 |
On 30/11/2013 01:06, Tim Chase wrote: > On 2013-11-30 00:59, Mark Lawrence wrote: >> 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 > > > It's way past my bedtime :) -- Python is the second best programming language in the world. But the best has yet to be invented. Christian Tismer Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Johannes Findeisen <mailman@hanez.org> |
|---|---|
| Date | 2013-11-30 02:16 +0100 |
| Message-ID | <mailman.3416.1385774268.18130.python-list@python.org> |
| In reply to | #60779 |
On Sat, 30 Nov 2013 01:08:28 +0000 Mark Lawrence wrote: > On 30/11/2013 01:06, Tim Chase wrote: > > On 2013-11-30 00:59, Mark Lawrence wrote: > >> 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 ;-) :) > 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
[toc] | [prev] | [next] | [standalone]
| From | Julio Schwarzbeck <julio@techfuel.net> |
|---|---|
| Date | 2013-12-01 17:01 -0800 |
| Message-ID | <mailman.3454.1385946084.18130.python-list@python.org> |
| In reply to | #60779 |
On 11/29/2013 04:31 PM, farhanken@gmail.com wrote:
> 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web