Search code examples
python-3.xinputcurly-braces

How to correct display of curly braces in an input prompt?


I have some basic code to roll dice in python3, my input field is producing curly braces instead of an desired integer. How do I get my integer to show in place of my curly braces?

For a personal project trying to better understand python. I haven't tried anything as I am still new to python and unsure of where to begin. Tried searching here, found things either too advanced for me to understand, or issues far more specific than I need.

from random import randint

def roll(sides, number):
    return [randint(1, sides) for i in range(number)]

sides = int(input("\nHow many sides do you want on your dice?:  "))
number = int(input("\nHow many {} sided dice do you want to roll?:  "))
results = roll(sides, number)

sides = int(sides)
number = int(sides)
print(results)

Expected result if I input 10 and 2 (as an example): How many sides do you want on your die?: 10 How many 10 sided die do you want to roll?: 2

Output is instead: How many sides do you want on your dice?: 10

How many {} sided dice do you want to roll?: 2

I apologize if this is a silly question, but I am unsure of where to start.

Apologies in advance if the formatting here is strange, this is my first post on here, and I am unsure of proper methods.


Solution

  • If you use Python 3.6 or above, you can use Formatted string literals using the "f" prefix, like so: f"your {var} variable". For instance:

    number = int(input(f"\nHow many {sides} sided dice do you want to roll?:  "))
    

    Or, you can explicitly use the format method:

    number = int(input("\nHow many {sides} sided dice do you want to roll?:  ".format(sides=sides)))
    

    or

    number = int(input("\nHow many {} sided dice do you want to roll?:  ".format(sides)))