-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.

Temba
  • 370
  • 2
  • 14
  • 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? – user99999 Nov 14 '22 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 – not a tshirt Nov 14 '22 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. – anaveragenoobie Nov 15 '22 at 10:28
  • Does this answer your question? [Format numbers to strings in Python](https://stackoverflow.com/questions/22617/format-numbers-to-strings-in-python) – tevemadar Nov 20 '22 at 13:50

2 Answers2

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 {}

Temba
  • 370
  • 2
  • 14
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?

  • This will get the job done here, but please be very careful when using eval. It can become a very sneaky security issue. – Jovan V Nov 14 '22 at 23:09
  • 1
    @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. – Ignatius Reilly Nov 14 '22 at 23:36
  • 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. – Tristan Bodding-Long Nov 15 '22 at 00:25
  • Thanks for the solution @TristanBodding-Long ! I'm trying to make an app with randomized math problems. – anaveragenoobie Nov 15 '22 at 10:31