Skip to content

Answer to print(9 +-+-+-+ 2) in Python? #122966

Discussion options

You must be logged in to vote

Hey @Vajeri , funky question :)

In the +-+-+-+ string, the leftmost is treated as binary operator (taking two arguments) and the others modify the sign of 2.

Starting with a simpler example:

a = 9 +- 2
a == 9 + (-2) == 7

Then, getting the result of the expression you provide is like solving one of these algebra exercises from school:

a = 9 +-+-+-+ 2
  = 9 + (- (+ (- (+ (- (+ 2))))))
  = 9 (- (+ (- (+ (- 2)))))
  = 9 (- (+ (- (- 2))))
  = 9 (- (+ 2))
  = 9 - 2 == 7

If this answers your question, please consider marking it as the answer :)

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Vajeri
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Programming Help Programming languages, open source, and software development.
3 participants