0

How do I make an operator a variable? for example I want to store the value 1 in operator '+' that is "+" = 1. But python is showing an error, what do I do? my project is this: while True:

current_number = int(input("Enter the number that you wish to be displayed: "))

 print(f"The current displayed number is: {current_number}")

 print("The value of '+' and '-' is limited to 5")

 n = input()

 if n == "+" or "++" or "+++" or "++++" or "+++++":

    if n == "+":

        print(current_number + 1)

    if n == "++":

        print(current_number + 2)

    if n == "+++":

        print(current_number + 3)

    if n == "++++":
        print(current_number + 4)

    if n == "+++++":
        print(current_number + 5)

 elif n == "-" or "--" or "---" or "----" or "-----":
    if n == "-":

        print(current_number - 1)

    if n == "--":

        print(current_number - 2)

    if n == "---":

        print(current_number - 3)

    if n == "----":

        print(current_number - 4)

    if n == "-----":

        print(current_number - 5)

I want to simplify the code by making "+" = 1, how do I do it?

John Gordon
  • 29,573
  • 7
  • 33
  • 58
Ayushje
  • 3
  • 6
  • 1
    Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – FLAK-ZOSO Apr 09 '22 at 18:00
  • `+` is not a valid variable name. – John Gordon Apr 09 '22 at 18:02
  • but i have to do it in any way – Ayushje Apr 09 '22 at 18:03
  • If your teacher told you to redefine `+` so that it's a valid expression on its own, they have set you an impossible task. Is this an assignment from April 1st that you're just now starting on? Or is it possible that you're misunderstanding the instructions? – Samwise Apr 09 '22 at 18:04
  • @Samwise i have redited my post please check it out – Ayushje Apr 09 '22 at 18:08
  • With the code it's *much* more clear what you were trying to do! Always include the code up front when you ask a question. :) – Samwise Apr 09 '22 at 18:13
  • `if n == "+" or "++" or "+++" or "++++" or "+++++"` This statement will _always_ be true... – John Gordon Apr 09 '22 at 18:16
  • The thing that made your phrasing of the question confusing, fwiw, is that the *string* `"+"` is *not* in any way the same as the *operator* `+`! The word "operator" should never have been anywhere near this question. Your question is really "how do I convert `"+"` into `1`, `"++"` into `2`, and so on", and the answer to that is simply `len()`. – Samwise Apr 09 '22 at 18:27

2 Answers2

0

Things other than [A-Za-z0-9_] are not allowed as names of variables

If you 100% need that, use a dictionary ie

specialVars={'+':1, "-":-1}

then call

specialVars['+']

edit: if you jut need to add 1 for each '+' and -1 for each '-' do this

print(current_number+n.count('+')-n.count('-'))
0

Use len() to count the number of characters in n:

while True:
    current_number = int(input("Enter the number that you wish to be displayed: "))
    print(f"The current displayed number is: {current_number}")
    n = input()
    if set(n) == {"+"}:
        print(current_number + len(n))
    elif set(n) == {"-"}:
        print(current_number - len(n))
Enter the number that you wish to be displayed: 37
The current displayed number is: 37
+++++
42

Note that with this approach there's no need to arbitrarily limit the number of characters, although you can still do that explicitly by rejecting inputs where len(n) > 5.

Your original version of the check for if the string contains all "+" or "-" doesn't work:

if n == "+" or "++" or "+++" or "++++" or "+++++":

because (n == "+") or ("++") will simply return "++" (which is true) if n == "+" is not True. A "correct" way to write this check would be:

if n in ("+", "++", "+++", "++++", "+++++"):

or more simply (since these specific strings are all substrings of "+++++":

if n in "+++++":

My version of the code does this instead:

if set(n) == {"+"}:

which works by converting n to a set (reducing it to only the unique characters) -- if n contains all "+"s, then its set is {"+"}. This works for any length of n.

Samwise
  • 68,105
  • 3
  • 30
  • 44