-1
import random q = random.randint(10, 100) w = random.randint(10, 100) e = (q, " * ", w) r = int(input(e)) 

This outputs (i.e):

>>> (60, ' * ', 24) 

I tried following this post but I faced an error.

I want output to atleast look like:

>>> (60 * 24) 

What I tried was doing

import random q = random.randint(10, 100) w = random.randint(10, 100) **e = (q + " * " + w)** r = int(input(e)) 

Which gave me the error.

4
  • 2
    which gave me the error What error? Please post the error text so we know what you mean. And does your code actually have the 2 stars at the beginning and end of the line like you have in the second example? Commented Nov 14, 2022 at 22:53
  • 1
    the problem is that you're trying to add an integer, a string and another integer together, which only works in javascript. What I think you want to do is either use fstrings, as suggested by Barnaby in his answer, or to convert the values into a string first with something like str(q) and str(w) before adding them together Commented Nov 14, 2022 at 22:54
  • @user99999 Yes, my bad. It was a dumb mistake and is solved now. And no I was trying to highlight the part I changed so it would be more obvious but it didn't work probably because it was already in code. Commented Nov 15, 2022 at 10:28
  • Does this answer your question? Format numbers to strings in Python Commented Nov 20, 2022 at 13:50

2 Answers 2

3

A great way to do this is with f-strings

e = f"{q} * {w}" 

You just need to start your string with an f, then include any variables in curly braces {}

Sign up to request clarification or add additional context in comments.

Comments

2

Your value e has mixed types. q and w are ints while the string is a string. Python prints the types in the tuple as their types. Those quotation marks are not in the values they see, they are a built-in helper in python's display

You will need to coerce all three into the same type to do something with them, eg

>>> eval(str(q) + ' * ' + str(w)) 1482 

That's the low level pointer, but higher level I need to ask, what are you trying to do?

4 Comments

This will get the job done here, but please be very careful when using eval. It can become a very sneaky security issue.
@JovanVuchkov this seems to be a safe case since the variables are just numbers created by the program. Anyway, I think eval is used here just to show that they can be operated as strings... maybe not the most intuitive example.
yeah I'd never encourage using eval in a program with user defined input. This is demonstrative of types while it's unclear what TC wants to do.
Thanks for the solution @TristanBodding-Long ! I'm trying to make an app with randomized math problems.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.