Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 17th, 2012, 09:12 AM
nbressler nbressler is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 3 nbressler User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 56 sec
Reputation Power: 0
Petals around the rose python script

Hi all,

I'm looking for a python code for the petal around the rose. Please Help!

Thanks,
Nina

Reply With Quote
  #2  
Old August 17th, 2012, 12:44 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,460 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 56 m 42 sec
Reputation Power: 403
My favorite python code.

oh, ok, this is some sort of dice game.
Are you willing to help write the program?

Why must it be in python?
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old August 17th, 2012, 07:14 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,460 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 56 m 42 sec
Reputation Power: 403
Uninspired petals around the rose in executable Iverson notation (j is available on android and iphone---the best calculator you can get)
Code:
   DICE=: [;._2 'one two three four five six '
   roseRing=: ({&DICE&.> ,: +/@:({&0 0 2 0 4 0)&.>)@:(6 ?@#~&.> 2 + ?@(#&5))

   roseRing 8
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│three│six  │six  │two  │three│one  │two  │three│
│one  │five │one  │three│five │two  │six  │three│
│     │one  │three│six  │four │one  │six  │six  │
│     │four │six  │two  │one  │three│     │three│
│     │     │     │     │six  │four │     │one  │
│     │     │     │     │six  │     │     │five │
├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
│2    │4    │2    │2    │6    │2    │0    │10   │
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘
   

I'll edit this to add spots.
...
Done.

Code:
DICE=: <"2' o'{~6 3 3$}.#:18582058114132935x
roseRing=: (+/@:({&0 0 2 0 4 0)&.> ,: ,.@:{&DICE&.>)@:(6 ?@#~&.> 2 + ?@(#&5))

Note 'example: the faces on a die'

     DICE  NB. (with a little editing help)
      ┌───┐         
      │o  │         
      │ o │         
      │  o│         
  ┌───┼───┼───┬───┐ 
  │   │  o│ooo│o o│ 
  │ o │   │   │ o │ 
  │   │o  │ooo│o o│ 
  └───┴───┼───┼───┘ 
          │o o│     
          │   │     
          │o o│     
          └───┘     
)



NB. rng ChooseNumberOfDiceForEachGame NUMBER_OF_GAMES
ChooseNumberOfDiceForEachGame=: (2:`+`[:`)(`(#&5))(`:6)
assert 6 3 5 3 2 5 3 3 (-:  ?. ChooseNumberOfDiceForEachGame) 8

NB. rng RollDice NUMBER_OF_FACES
RollDice=: conjunction define
rng=. u
FACES=. n
y u@# FACES
)

assert 0 2 4 (-:  (?. RollDice 6)) 3

grab_2_to_6_dice=: ? ChooseNumberOfDiceForEachGame
roll_6_sided_dice=: ? RollDice 6

Note 'example: the rolls for each of 5 games are boxed'

   The index origin is 0.

     ([: roll_6_sided_dice each grab_2_to_6_dice) 5
  ┌─────────┬─────────┬───────────┬─────────┬───────┐
  │4 0 0 2 5│3 5 1 1 2│5 3 5 2 0 5│4 5 2 2 2│1 3 1 0│
  └─────────┴─────────┴───────────┴─────────┴───────┘
)

score=: [: +/ {&0 0 2 0 4 0
assert  6  (-: score)  i. 6

petalsAroundTheRose=: (score each ,: ,.@:{&DICE each)@:([: roll_6_sided_dice each grab_2_to_6_dice)

Note 'example'
   petalsAroundTheRose 12
┌─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┐
│2    │0    │4    │0    │4    │6    │0    │10   │6    │10   │2    │6    │
├─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┤
│┌───┐│┌───┐│┌───┐│┌───┐│┌───┐│┌───┐│┌───┐│┌───┐│┌───┐│┌───┐│┌───┐│┌───┐│
││  o│││ooo│││ooo│││ooo│││o  │││  o│││   │││ooo│││o  │││   │││o  │││o o││
││   │││   │││   │││   │││ o │││   │││ o │││   │││ o │││ o │││ o │││ o ││
││o  │││ooo│││ooo│││ooo│││  o│││o  │││   │││ooo│││  o│││   │││  o│││o o││
│├───┤│├───┤│├───┤│├───┤│├───┤│├───┤│├───┤│├───┤│├───┤│├───┤│├───┤│├───┤│
││o  │││  o│││o o│││o o│││o  │││o o│││ooo│││  o│││o o│││o  │││o o│││  o││
││ o │││   │││ o │││   │││ o │││ o │││   │││   │││ o │││ o │││   │││   ││
││  o│││o  │││o o│││o o│││  o│││o o│││ooo│││o  │││o o│││  o│││o o│││o  ││
│└───┘│└───┘│├───┤│├───┤│└───┘│├───┤│├───┤│├───┤│├───┤│├───┤│├───┤│├───┤│
│     │     ││o o│││o o││     ││   │││ooo│││   │││  o│││o o│││o o│││  o││
│     │     ││   │││   ││     ││ o │││   │││ o │││   │││ o │││   │││   ││
│     │     ││o o│││o o││     ││   │││ooo│││   │││o  │││o o│││o o│││o  ││
│     │     │└───┘│├───┤│     │├───┤│├───┤│├───┤│├───┤│├───┤│└───┘│├───┤│
│     │     │     ││  o││     ││   │││   │││o  │││ooo│││o o││     ││o  ││
│     │     │     ││   ││     ││ o │││ o │││ o │││   │││ o ││     ││ o ││
│     │     │     ││o  ││     ││   │││   │││  o│││ooo│││o o││     ││  o││
│     │     │     │└───┘│     │├───┤│└───┘│├───┤│├───┤│└───┘│     │└───┘│
│     │     │     │     │     ││o  ││     ││o o│││   ││     │     │     │
│     │     │     │     │     ││ o ││     ││ o │││ o ││     │     │     │
│     │     │     │     │     ││  o││     ││o o│││   ││     │     │     │
│     │     │     │     │     │├───┤│     │├───┤│├───┤│     │     │     │
│     │     │     │     │     ││  o││     ││o o│││o o││     │     │     │
│     │     │     │     │     ││   ││     ││ o │││   ││     │     │     │
│     │     │     │     │     ││o  ││     ││o o│││o o││     │     │     │
│     │     │     │     │     │└───┘│     │└───┘│└───┘│     │     │     │
└─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┘
)


Interesting, the program is about the same number of characters with text or spots. If I had used classical Mongolian or some of the US Indian languages the program would be shorter with spots. Of course, I finagled a bit to compress the spots. The binary representation of 18582058114132935 might help your understanding. (which you can find in j with
#: 18582058114132935x
)

Last edited by b49P23TIvg : August 21st, 2012 at 11:14 AM. Reason: promised change installed

Reply With Quote
  #4  
Old August 21st, 2012, 10:09 AM
nbressler nbressler is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 3 nbressler User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 56 sec
Reputation Power: 0
Petals around the Rose - Python code

Thanks for your reply b49P23TIvg.

Here's what I have but I'm getting a syntax error and can't figure it out. Please help!


import random
def printDice(diceList):
upperLine=" _____ _____ _____ _____ _____"
line1="|"
line2="|"
line3="|"
lowerLine=" ----- ----- ----- ----- -----"
for i in range(len(diceList)):
if(diceList[i]==1):
line1+=" "
elif(diceList[i]==2):
line1+="* "
elif(diceList[i]==3):
line1+="* "
elif(diceList[i]==4):
line1+="* *"
elif(diceList[i]==5):
line1+="* *"
else:
line1+="* *"
if(i==4):
line1+="|"
else:
line1+="| |"
for i in range(len(diceList)):
if(diceList[i]==1):
line2+=" * "
elif(diceList[i]==2):
line2+=" "
elif(diceList[i]==3):
line2+=" * "
elif(diceList[i]==4):
line2+=" "
elif(diceList[i]==5):
line2+=" * "
else:
line2+="* *"
if(i==4):
line2+="|"
else:
line2+="| |"
for i in range(len(diceList)):
if(diceList[i]==1):
line3+=" "
elif(diceList[i]==2):
line3+=" *"
elif(diceList[i]==3):
line3+=" *"
elif(diceList[i]==4):
line3+="* *"
elif(diceList[i]==5):
line3+="* *"
else:
line3+="* *"
if(i==4):
line3+="|"
else:
line3+="| |"
print upperLine
print line1
print line2
print line3
print lowerLine

tellMe="N"
print

print "The purpose of the game is to figure out the rule."
print "I can tell you three things:\n1. The name of the game is petals around a rose, the name is important.\n2. I can tell you whether or not your guess is right and the score of the dice.\n3. I can tell you that the score is always even or 0"
print
go="Y"
wrongGuesses=0
while(go=="Y"):
diceList=[]
score=0
rightWrong="N"
for i in range(5):
diceList.append(random.randrange(1,7))
for i in range(5):
if(diceList[i]==3):
score+=2
elif(diceList[i]==5):
score+=4
printDice(diceList)
print
while(rightWrong=="N"):
guess=raw_input("What is your guess? ")
if(guess%2==1):
break
if(guess!=score):
print "Wrong"
wrongGuesses+=1
tellMe=raw_input("Tell you (Y or N)? ")
tellMe=tellMe.upper()
if(tellMe=="Y"):
print "The score was "+str(score)+"."
rightWrong="Y"
else:
rightWrong="Y"
print "Right"
if(wrongGuesses%13==0 and wrongGuesses!=0):
print"The name is very important."
if((wrongGuesses==30) and (wrongGuesses!=0)):
print "The maximum score is 20."
print

Reply With Quote
  #5  
Old August 21st, 2012, 11:02 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,460 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 4 Days 6 h 56 m 42 sec
Reputation Power: 403
I've made a few changes marked by comments, and indented the code as you might have intended. It seems to work. Please follow the code tag advice at the link in my signature for your future python posts. Python white space is a syntactical element!
Code:
import random

def printDice(diceList):
    '''
        prettier dice
            +---+ +---+ +---+ +---+ +---+ +---+
            |   | |*  | |*  | |* *| |* *| |* *|
            | * | |   | | * | |   | | * | |* *|
            |   | |  *| |  *| |* *| |* *| |* *|
            +---+ +---+ +---+ +---+ +---+ +---+
    '''
    upperLine=" _____ _____ _____ _____ _____"
    line1=" |"                          # inserted preceding space
    line2=" |"
    line3=" |"
    lowerLine=" ----- ----- ----- ----- -----"
    ################# these strings need to be the same length!!!
    for i in range(len(diceList)):################################################################
        if(diceList[i]==1):   line1+="   "
        elif(diceList[i]==2): line1+="*  "
        elif(diceList[i]==3): line1+="*  "
        elif(diceList[i]==4): line1+="* *"
        elif(diceList[i]==5): line1+="* *"
        else:                 line1+="* *"
        if(i==4): line1+="|"
        else: line1+="| |"
    for i in range(len(diceList)):
        if(diceList[i]==1):   line2+=" * "
        elif(diceList[i]==2): line2+="   "
        elif(diceList[i]==3): line2+=" * "
        elif(diceList[i]==4): line2+="   "
        elif(diceList[i]==5): line2+=" * "
        else:                 line2+="* *"
        if(i==4): line2+="|"
        else: line2+="| |"
    for i in range(len(diceList)):
        if(diceList[i]==1):   line3+="   "
        elif(diceList[i]==2): line3+="  *"
        elif(diceList[i]==3): line3+="  *"
        elif(diceList[i]==4): line3+="* *"
        elif(diceList[i]==5): line3+="* *"
        else:                 line3+="* *"
        line3 += ('| |','|',)[4==i]
    print upperLine
    print line1
    print line2
    print line3
    print lowerLine

tellMe="N"
print

print'''The purpose of the game is to figure out the rule.
I can tell you three things:
1. The name of the game is petals around a rose, the name is important.
2. I can tell you whether or not your guess is right and the score of the dice.
3. I can tell you that the score is always even or 0
'''
go="Y"
wrongGuesses=0
while(go=="Y"):
    diceList=[]
    score=0
    rightWrong="N"
    for i in range(5): diceList.append(random.randrange(1,7))
    for i in range(5):
        if(diceList[i]==3): score+=2
        elif(diceList[i]==5): score+=4
    printDice(diceList)
    print
    while(rightWrong=="N"):
        stringguess=raw_input("What is your guess? ")
        try: #### raw_input is good function to use, but returns a string that you need to convert
            #### Actually, I'd read from sys.stdin for compatibility with python3
            guess = int(stringguess)
        except ValueError:
            print'Needed an integral guess.  Substituting guess of 0.'
            guess = 0
        if(guess%2==1): break
        if(guess!=score):
            print "Wrong"
            wrongGuesses+=1
            tellMe=raw_input("Tell you (Y or N)? ")
            tellMe=tellMe.upper()
        if(tellMe=="Y"):
            print "The score was "+str(score)+"."
            rightWrong="Y"
        else:
            rightWrong="Y"
        print "Right"                   # this statement might be out of place in particular
        if(wrongGuesses%13==0 and wrongGuesses!=0):
            print"The name is very important."
    if((wrongGuesses==30) and (wrongGuesses!=0)):
        print "The maximum score is 20."
        print

Last edited by b49P23TIvg : August 21st, 2012 at 12:32 PM.

Reply With Quote
  #6  
Old August 21st, 2012, 01:21 PM
nbressler nbressler is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 3 nbressler User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 35 m 56 sec
Reputation Power: 0
This is awesome. Thank you so much.


Quote:
Originally Posted by b49P23TIvg
I've made a few changes marked by comments, and indented the code as you might have intended. It seems to work. Please follow the code tag advice at the link in my signature for your future python posts. Python white space is a syntactical element!
Code:
import random

def printDice(diceList):
    '''
        prettier dice
            +---+ +---+ +---+ +---+ +---+ +---+
            |   | |*  | |*  | |* *| |* *| |* *|
            | * | |   | | * | |   | | * | |* *|
            |   | |  *| |  *| |* *| |* *| |* *|
            +---+ +---+ +---+ +---+ +---+ +---+
    '''
    upperLine=" _____ _____ _____ _____ _____"
    line1=" |"                          # inserted preceding space
    line2=" |"
    line3=" |"
    lowerLine=" ----- ----- ----- ----- -----"
    ################# these strings need to be the same length!!!
    for i in range(len(diceList)):################################################################
        if(diceList[i]==1):   line1+="   "
        elif(diceList[i]==2): line1+="*  "
        elif(diceList[i]==3): line1+="*  "
        elif(diceList[i]==4): line1+="* *"
        elif(diceList[i]==5): line1+="* *"
        else:                 line1+="* *"
        if(i==4): line1+="|"
        else: line1+="| |"
    for i in range(len(diceList)):
        if(diceList[i]==1):   line2+=" * "
        elif(diceList[i]==2): line2+="   "
        elif(diceList[i]==3): line2+=" * "
        elif(diceList[i]==4): line2+="   "
        elif(diceList[i]==5): line2+=" * "
        else:                 line2+="* *"
        if(i==4): line2+="|"
        else: line2+="| |"
    for i in range(len(diceList)):
        if(diceList[i]==1):   line3+="   "
        elif(diceList[i]==2): line3+="  *"
        elif(diceList[i]==3): line3+="  *"
        elif(diceList[i]==4): line3+="* *"
        elif(diceList[i]==5): line3+="* *"
        else:                 line3+="* *"
        line3 += ('| |','|',)[4==i]
    print upperLine
    print line1
    print line2
    print line3
    print lowerLine

tellMe="N"
print

print'''The purpose of the game is to figure out the rule.
I can tell you three things:
1. The name of the game is petals around a rose, the name is important.
2. I can tell you whether or not your guess is right and the score of the dice.
3. I can tell you that the score is always even or 0
'''
go="Y"
wrongGuesses=0
while(go=="Y"):
    diceList=[]
    score=0
    rightWrong="N"
    for i in range(5): diceList.append(random.randrange(1,7))
    for i in range(5):
        if(diceList[i]==3): score+=2
        elif(diceList[i]==5): score+=4
    printDice(diceList)
    print
    while(rightWrong=="N"):
        stringguess=raw_input("What is your guess? ")
        try: #### raw_input is good function to use, but returns a string that you need to convert
            #### Actually, I'd read from sys.stdin for compatibility with python3
            guess = int(stringguess)
        except ValueError:
            print'Needed an integral guess.  Substituting guess of 0.'
            guess = 0
        if(guess%2==1): break
        if(guess!=score):
            print "Wrong"
            wrongGuesses+=1
            tellMe=raw_input("Tell you (Y or N)? ")
            tellMe=tellMe.upper()
        if(tellMe=="Y"):
            print "The score was "+str(score)+"."
            rightWrong="Y"
        else:
            rightWrong="Y"
        print "Right"                   # this statement might be out of place in particular
        if(wrongGuesses%13==0 and wrongGuesses!=0):
            print"The name is very important."
    if((wrongGuesses==30) and (wrongGuesses!=0)):
        print "The maximum score is 20."
        print

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Petals around the rose python script

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap