Jump to content

Help with iterative Python statement

- - - - -

  • Please log in to reply
2 replies to this topic

#1
asdf76

asdf76

    Newbie

  • Members
  • Pip
  • 1 posts
import cgi

formData = cgi.FieldStorage()


import random


visitorName = formData["formName"].value

visitorGuess = formData["formGuess"].value

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)

total = int(die1+die2+die3+die4+die5)


print "Content-type: text/html"

print

print """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

       <html xmlns="http://www.w3.org/1999/xhtml">

       <head>

       <title>Dice Game</title>

       </head>

       <body>

       <h1>Dice Game</h1>"""

print "<p>Thanks for playing, " +visitorName+ ". </p>"

print "<p>You bet the total would be " +str(visitorGuess)+ ". </p>"

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

if die1==1:

    print "<p><img src='Images/dice-1.png' alt='dice1' /></p>"

elif die1==2:

    print "<p><img src='Images/dice-2.png' alt='dice2'  /></p>"

elif die1==3:

    print "<p><img src='Images/dice-3.png' alt='dice3'  /></p>"

elif die1==4:

    print "<p><img src='Images/dice-4.png' alt='dice4'  /></p>"

elif die1==5:

    print "<p><img src='Images/dice-5.png' alt='dice5'  /></p>"

if die2==1:

    print "<p><img src='Images/dice-1.png' alt='dice1' /></p>"

elif die2==2:

    print "<p><img src='Images/dice-2.png' alt='dice2'  /></p>"

elif die2==3:

    print "<p><img src='Images/dice-3.png' alt='dice3'  /></p>"

elif die2==4:

    print "<p><img src='Images/dice-4.png' alt='dice4'  /></p>"

elif die2==5:

    print "<p><img src='Images/dice-5.png' alt='dice5'  /></p>"

if die3==1:

    print "<p><img src='Images/dice-1.png' alt='dice1' /></p>"

elif die3==2:

    print "<p><img src='Images/dice-2.png' alt='dice2'  /></p>"

elif die3==3:

    print "<p><img src='Images/dice-3.png' alt='dice3'  /></p>"

elif die3==4:

    print "<p><img src='Images/dice-4.png' alt='dice4'  /></p>"

elif die3==5:

    print "<p><img src='Images/dice-5.png' alt='dice5'  /></p>"

if die4==1:

    print "<p><img src='Images/dice-1.png' alt='dice1' /></p>"

elif die4==2:

    print "<p><img src='Images/dice-2.png' alt='dice2'  /></p>"

elif die4==3:

    print "<p><img src='Images/dice-3.png' alt='dice3'  /></p>"

elif die4==4:

    print "<p><img src='Images/dice-4.png' alt='dice4'  /></p>"

elif die4==5:

    print "<p><img src='Images/dice-5.png' alt='dice5'  /></p>"

if die5==1:

    print "<p><img src='Images/dice-1.png' alt='dice1' /></p>"

elif die5==2:

    print "<p><img src='Images/dice-2.png' alt='dice2'  /></p>"

elif die5==3:

    print "<p><img src='Images/dice-3.png' alt='dice3'  /></p>"

elif die5==4:

    print "<p><img src='Images/dice-4.png' alt='dice4'  /></p>"

elif die5==5:

    print "<p><img src='Images/dice-5.png' alt='dice5'  /></p>"

if total == visitorGuess:

    print "<p> You Won!!! </p>"

elif total != visitorGuess:

    print "<p> Sorry you lost. </p>"

print """

       </body>

       </html>"""



I need to be able to print out an image of the 5 random dice results, but I need to do it in a more efficient way than I have in the coding above. here are the instructions from my assignment bellow. I don't really understand what to do.

Step 5 – Dice Images
• However, you can create a more efficient
algorithm to display the 5 images of the dice
• This efficient algorithm will have only one
<img> statement and will make use of an
iterative Python statement
• This single <img> statement will look a bit
different from the one above
• You must create this efficient algorithm

#2
common_man

common_man

    Newbie

  • Members
  • Pip
  • 8 posts
Hi,
Not sure what exactly you need, but you can try inserting the logical 'OR' operator. I have given the example for the first case below. You can do the same for other cases.

asdf76 said:



if die1==1 or die2==1 or die3==1 or die4==1 or die5==1:

    print "<p><img src='Images/dice-1.png' alt='dice1' /></p>"




#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Can you not do something like this?
print "die 1: <img src=" + die1 + ".png/>"
print "die2: <...

Of course this is not real Python, the simplicity of this is true however.

You could store all six die results in an array, and display the six die pictures in one single call.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users