Showing variable in a pop-up window

Question:

I have a small 2D game and I want to print a message when it’s finished. The code is in Python and I used both Pygame and Tkinter for 2D graphic.
When the game is finished it shows the message below:

messagebox.showinfo('Finish','Mouse ate all cheese slices.nPolicies saved to file.')

I have a variable called steps and I need to show it in that window but it raise an error when I tried it like this:

messagebox.showinfo('Finish','Mouse ate all cheese slices after {steps}.nPolicies saved to file.')

Update: Using f-string solved the problem:

messagebox.showinfo('Finish',f'Mouse ate all cheese slices after {steps}.nPolicies saved to file.')
Asked By: Ismail

||

Answers:

You need to use an f string. Your code should look something like this:

messagebox.showinfo('Finish',f'Mouse ate all cheese slices after {steps}.nPolicies saved to file.')

Here’s a link to learn more ways to use this sort of formatting:
Python F-Strings

Answered By: Oscar Solis

Try to use an f-string:

messagebox.showinfo('Finish',f'Mouse ate all cheese slices after {steps}.nPolicies saved to file.')
Answered By: Pork Lord
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.