Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Based off my Java Petals Around the RosePetals Around the Rose, I thought it would be good practice to write something again in a different language.

I wrote it to be based of standard conventions as close as possible. Because I'm a Java person, I might have added a ; somewhere in my codemight have added a ; somewhere in my code, or perhaps accidentally forgot to follow conventions somewhere.

Based off my Java Petals Around the Rose, I thought it would be good practice to write something again in a different language.

I wrote it to be based of standard conventions as close as possible. Because I'm a Java person, I might have added a ; somewhere in my code, or perhaps accidentally forgot to follow conventions somewhere.

Based off my Java Petals Around the Rose, I thought it would be good practice to write something again in a different language.

I wrote it to be based of standard conventions as close as possible. Because I'm a Java person, I might have added a ; somewhere in my code, or perhaps accidentally forgot to follow conventions somewhere.

Tweeted twitter.com/StackCodeReview/status/677463934295801856
edited body
Source Link
TheCoffeeCup
  • 9.5k
  • 4
  • 38
  • 96

Because the language is Python, I named isit "Pythons Around the Rose", and everywhere in my code, the word "rose" would be replaced with "python".

Because the language is Python, I named is "Pythons Around the Rose", and everywhere in my code, the word "rose" would be replaced with "python".

Because the language is Python, I named it "Pythons Around the Rose", and everywhere in my code, the word "rose" would be replaced with "python".

Source Link
TheCoffeeCup
  • 9.5k
  • 4
  • 38
  • 96

Pythons Around the Rose

Based off my Java Petals Around the Rose, I thought it would be good practice to write something again in a different language.

Because the language is Python, I named is "Pythons Around the Rose", and everywhere in my code, the word "rose" would be replaced with "python".

Also, "Potentate" is replaced with "Pytentate".

Code:

import random

def display_help():
    example = get_dice_result()
    print("The name of the game is Pythons Around the Rose.",
          "The name is important. I will roll five dice,",
          "and I will tell you how many pythons there are.",
          "\nFor example:", sep = "\n")
    print_dice(example)
    print("Will result in ", get_answer(example), ".", sep = "")
    print("\nIf you answer correctly 8 times in a row, you",
          "will be declared a \"Pytentate of the Rose\".", sep = "\n")

def play():
    streak = 0
    while True:
        dice = get_dice_result()
        if play_once(dice):
            streak += 1
        else:
            streak = 0
        if streak == 8:
            print('You are now declared a "Pytentate of the Rose"!')
            break;
    print("Thank you for playing!")

def get_dice_result():
    result = [get_dice_roll(), get_dice_roll(),
              get_dice_roll(), get_dice_roll(), get_dice_roll()]
    return result

def get_dice_roll():
    return random.randrange(6) + 1

def play_once(results):
    print("How many pythons here?")
    print_dice(results)
    guess = get_guess()
    answer = get_answer(results)
    if guess == answer:
        print("Correct!")
        return True
    print("Incorrect. The answer is ", answer, ".", sep = "")
    return False

def get_guess():
    guess = 0
    valid = False
    while not valid:
        try:
            guess = int(input("> "))
            valid = True
        except:
            print("\nOops! That is not a number. Try again: ")
    return guess

def get_answer(dice):
    answer = 0
    for i in dice:
        if i == 3:
            answer += 2
        elif i == 5:
            answer += 4
    return answer;

def print_dice(dice):
    rows = ["|", "|", "|"]
    for i in dice:
        if i == 1:
            rows[0] += "       |"
            rows[1] += "   .   |"
            rows[2] += "       |"
        elif i == 2:
            rows[0] += " .     |"
            rows[1] += "       |"
            rows[2] += "     . |"
        elif i == 3:
            rows[0] += " .     |"
            rows[1] += "   .   |"
            rows[2] += "     . |"
        elif i == 4:
            rows[0] += " .   . |"
            rows[1] += "       |"
            rows[2] += " .   . |"
        elif i == 5:
            rows[0] += " .   . |"
            rows[1] += "   .   |"
            rows[2] += " .   . |"
        elif i == 6:
            rows[0] += " .   . |"
            rows[1] += " .   . |"
            rows[2] += " .   . |"
    print(rows[0], rows[1], rows[2], sep = "\n")

def main():
    display_help()
    play()

if __name__ == '__main__':
    main()

I wrote it to be based of standard conventions as close as possible. Because I'm a Java person, I might have added a ; somewhere in my code, or perhaps accidentally forgot to follow conventions somewhere.