Search code examples
pythonpython-3.xoperatorspython-3.1

Operators from list is still showing quotation marks when printed (Python 3.1)


When I'm coding, I choose a random value from a list and print it alongside two numbers to make a sum. However, the value from the list still shows the quotation marks and I don't understand why. The code is:

import random
level = input('Please choose either easy, medium or hard')
if level == 'easy':
    num1 = random.randint(1,5)
    num2 = random.randint(1,5)
    #Chooses a random operator from the list
    op = random.choice(['+', '-', '*'])
    #Arranges it so the larger number is printed first
    if num1 > num2:
        sum1 = (num1, op, num2)
    else:
        sum1 = (num2, op, num1)
    #Prints the two numbers and the random operator
    print(sum1)

I try running this code and the result I am given is:

(4, '*', 3)

When I want it to appear as:

4*3

Those numbers are randomly generated also but work fine. Does anyone know how to solve this problem?


Solution

  • You are printing a list, which produces this format. In order to get your desired output you could join the list with an empty delimiter:

    print (''.join(sum1))
    

    EDIT:

    Just noticed you have the operands as ints, not strings. To use this technique, you should convert all the elements to strings. E.g.:

    print (''.join([str(s) for s in sum1]))