0

So I’ve created a very basic program that rolls a dice a given number of times, and then displays who wins the game. I’ve tried to print the results as they go along with the roll #.

The problem I’m having is the output seems weirdly, an indecipherably (at least to my eyes) inconsistent (ie: sometimes it will show the roll #, sometimes it won’t.

Besides how much cleaner I could make this code, what am I doing that’s producing inconsistent roll # printing?

Code as follows:

import random

print("Let's play a little game!")
print("who is the most popular number on a die?")
print("Lets find out!")

def HighRolla():
    ones = 0
    twos = 0
    threes = 0
    fours = 0
    fives = 0
    sixes = 0
    tries = 1

    num_rolls = int(input("How many guesses?"))

    for roll in range(num_rolls):
        ran_num = random.randint(1, 6)
        if ran_num == 1:
            print(f"Roll {tries}: One for the ones")
            ones += 1
        elif ran_num == 2:
            print("Roll {tries}: Two Two look at my shoe")
            twos += 1
        elif ran_num == 3:
            print("Roll {tries}: THREE!! I'm free'")
            threes += 1
        elif ran_num == 4:
            print("Roll {tries}: F0ar equals m0ar")
            fours += 1
        elif ran_num == 5:
            print("Roll {tries}: Five five don't step in a bee hive'")
            fives += 1
        elif ran_num == 6:
            print("Roll {tries}: Super Sixes - Yeharrrr!")
            sixes += 1
    rolls += 1
    tries += 1  
    print(f"Final scores are: \n1: {ones}\n2: {twos}\n3: {threes}\n4: {fours}\n5: {fives}\n6: {sixes}")
    
    
HighRolla()’’’
2
  • 4
    The print statements for 2 through 6 are missing the f at the beginning of the string, so it prints the actual text {tries} instead of the value. Commented Feb 10, 2022 at 21:46
  • Your edit put the rolls and tries outside the for-loop. Please post your actual code as it's currently written and don't change it. Commented Feb 10, 2022 at 21:54

2 Answers 2

1

The problem is that you don't use f-Strings correctly. If you want your variables to be replaced inside curly brackets you need to put an "f" in front of your string.

You could also use a dictionary or an array to make your code clearer and more readable, like so:

import random

print("Let's play a little game!")
print("who is the most popular number on a die?")
print("Lets find out!")

def HighRolla():
    roll_counts = [0] * 6 # Creates an array of 6 zeroes
    roll_messages = [
        "One for the ones",
        "Two Two look at my shoe",
        "THREE!! I'm free'",
        "F0ar equals m0ar",
        "Five five don't step in a bee hive'",
        "Super Sixes - Yeharrrr!"
    ]

    num_rolls = int(input("How many guesses?"))

    for roll in range(num_rolls):
        ran_num = random.randint(1, 6)
        roll_counts[ran_num-1] += 1
        print(f"Roll {roll}: {roll_messages[ran_num-1]}")
    
    print(f"Final scores are:")
    print("\n".join([f"{idx+1}: {roll_count}" for idx, roll_count in enumerate(roll_counts)]))
    
HighRolla()
Sign up to request clarification or add additional context in comments.

2 Comments

Oh wow how embarrassing that I didn’t pick that up. Thanks for reading over this, and more importantly showing me other ways to achieve my result with dictionaries. Thanks guys and girls
No worries, please accept an answer if your problem is solved.
1

To answer your question: You forgot the f at the start of the strings in the print statements for 2 through 6. Therefore, it prints the actual text {tries} instead of the value.

In addition, I'd recommend:

  • Using a dictionary instead of all of those ifs.
  • Note that you didn't initialize rolls before using it, and you also set tries to 1 instead of 0. There is also no use for rolls.
  • Using a defaultdict for counting the results, instead of initializing a separate variable for each number. defaultdict returns a new dictionary-like object, where the first argument provides the initial value for the default_factory.
  • Method names in python should also be lowercase and seperated by _.

Implementing those steps would make your code much shorter and more pythonic.

Putting it all together:

import random
from collections import defaultdict

print("Let's play a little game!")
print("who is the most popular number on a die?")
print("Lets find out!")

def high_rolla():
    num_rolls = int(input("How many guesses?"))
    random_results = defaultdict(int)
    tries = 0
    random_to_print = {1: "One for the ones",
                       2: "Two Two look at my shoe",
                       3: "THREE!! I'm free'",
                       4: "F0ar equals m0ar",
                       5: "Five five don't step in a bee hive'",
                       6: "Super Sixes - Yeharrrr!"}
    for roll in range(num_rolls):
        ran_num = random.randint(1, 6)
        tries += 1
        print(f"Roll {tries}: " + random_to_print[ran_num])
        random_results[ran_num] += 1  
    print("Final scores are: ")
    for key, value in random_results.items():
        print(str(key) + " : " + str(value))
    
    
high_rolla()

Output:

Let's play a little game!
who is the most popular number on a die?
Lets find out!
How many guesses?3
Roll 1: Two Two look at my shoe
Roll 2: Two Two look at my shoe
Roll 3: One for the ones
Final scores are: 
2 : 2
1 : 1

2 Comments

F-strings are string literals and are evaluated when declared, so won't all of those always print with tries = 0?
@CrazyChucky Your'e right, I wasn't aware of that. I've updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.