88user 88user - 5 months ago 14
Python Question

combining a string with a variable in a variable

i'm a complete beginner and i'm kinda stuck on something. i'm trying to make something that you give a number to, it takes it and divides it in coins that have 5pts and coins that have 1pt. for example if you say i have 41pts it will give you 8 5pt coins and 1 1pt coin. for now i want to say if it's divisible by 5 then give 5pt coins and if it's not give 1pt coins. i've gotten this far

from tkinter import *
from tkinter import ttk
import tkinter as tk
def get_text():
global number
numbers = num_textbox.get()
number = int(numbers)
print(number)
root = Tk()
root.title("Number Cent Divider")
root.geometry("365x155")
num_col_mat = Label(root, text="Your number:")
num_col_mat.pack()
num_textbox = Entry(root, bd=1)
num_textbox.pack()
enter_button = Button(root, text="Enter", command=add_text)
enter_button.pack()
root.mainloop()
def popup():
popup = tk.TopLevel()
popup.wm_title("answer")
answer = Label(popup, text=answer)
answer.pack
B1 = ttk.Button(popup, text="Ok", command=popup.destroy)
B1.pack()
str(number)
if (number[-1] == 5 or number[-1] == 0):
answer = num


the code isn't complete yet and has errors probably everywhere since its not tested. however my problem here is i don't know how to put in a string after the var and put that whole thing in a var.like this?

answer = (number, "5pt coins")


if it's anything unlike this (which it probably is) i need help with it.
basically how do i combine a string with a variable in a different variable??

Answer Source

You can convert the number into string with built in str function then concatenate them with + operator or use string formatting like @Błotosmętek suggested

number = 10
answer = str(number) + " 5pt coins"
# '10 5pt coins'