-3

I am trying to print the equation with the variables

I have already tried to put all symbols in quotes

import random
import random
def ask():
    a = raw_input("do you want the equation to be easy, medium, or hard: ")
    b = int(raw_input("what is the number that you want to be the answer: "))
    if(a == "easy"):
        d = random.randint(1, 10)
        e = random.randint(2, 5)
        round(b)
        print C = b - d + e  - (e/2) + ((d - e) + e/2)

I wanted it to print out the equation with all the variables and symbols when i type this in i get a syntax error

R4444
  • 2,016
  • 2
  • 19
  • 30
Alex
  • 23
  • 1
  • 8
  • 7
    Possible duplicate of [How can I print variable and string on same line in Python?](https://stackoverflow.com/questions/17153779/how-can-i-print-variable-and-string-on-same-line-in-python) – joel Jul 06 '19 at 19:21

3 Answers3

1

try to put your equation in str() first,then print string so that it will display equation before result. then print out results

Arjunsai
  • 176
  • 1
  • 7
1

You cannot print out strings not in quotes. Put the bits you want to print out exactly as written in quotes, and print variables as is. For example:

print 'C =', b, '-', d, '+', e, '-', (e/2), '+', ((d - e/2)

Play around with that and see how you go. You'll want to think about how to do it differently if e.g. d-e/2 is negative.

Also round(b) will do nothing, it does not operate in-place.

M Somerville
  • 4,499
  • 30
  • 38
1

Here's what I think you want as a full solution. It accepts a single equation string as an input It then fills out that equation with the input variables, prints the resulting equation, and then evaluates it to provide a result:

import random

equation = "b - c + e  - (e/2) + ((d- e) + e/2)"

b = 12
c = 24
d = random.randint(1, 10)
e = random.randint(2, 5)

# Expand the vlaues into the equation
equation = equation.replace('b', str(b)).replace('c', str(c)).replace('d', str(d)).replace('e', str(e))

# Print the equation
print "C = " + equation

# Evaluate the equation and print the result
C = eval(equation)
print "C = " + str(C)

Sample result:

C = 12 - 24 + 2  - (2/2) + ((6- 2) + 2/2)
C = -6

This code is just a demonstration of what can be done. You could take these ideas and generalize this to expand a map of variable names and values into an arbitrary expression without hard-coding the variable names. The map and equation could come, for example, from a file.

CryptoFool
  • 21,719
  • 5
  • 26
  • 44