#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    3
    Rep 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
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    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 and Makefiles!
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    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
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    3
    Rep 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
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,931
    Rep Power
    481
    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.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0
    This is awesome. Thank you so much.


    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

IMN logo majestic logo threadwatch logo seochat tools logo