Showing variable in a pop-up window

Viewed 48

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.')
2 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

Try to use an f-string:

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