-2

I tried to make a code and i explained it down there. I cant figure out whats wrong please help. I am trying to let the user choose 2 numbers and an operator, use the operator on these two numbers and to print out different things if the final result is more or less than 100.

I am new to python so i couldnt exactly see whats wrong, please help me to fix that.

This is the error i get;

Traceback (most recent call last): File "C:\Users\Selmin\PycharmProjects\CodeExperience\Input Game.py", line 29, in total_result = totalresult(user_number1, user_number2) TypeError: totalresult() takes 0 positional arguments but 2 were given

# make an input command asking the user to answer with 2 numbers and ask which operator they want to use.
# if its more or less than 100, print different things.

def totalresult():
  if user_operator == "+":
      print(user_number1 + user_number2)
  elif user_operator == "-":
      if user_number1 > user_number2:
       print(int(user_number1 - int(user_number2)))
      else:
       print(int(user_number2 - int(user_number2)))

  if user_operator == "*":
      print(int(user_number1 * int(user_number2)))

  if user_operator == "/":
      while user_number1 > user_number2:
          print(int(user_number1 / int(user_number2)))
      else:
          print(int(user_number2 / int(user_number2)))
          return


user_number1 = int(input("Please enter a number. "))
user_number2 = int(input("Please enter another number. "))

user_operator = input("Please enter an operator. ")
total_result = totalresult(user_number1, user_number2)


if user_operator == "+":
  print(user_number1 + user_number2)
  total_result = int(user_number1 + int(user_number2))
while user_operator == "-":
  if user_number1 > user_number2:
      print(user_number1 - user_number2)
  else:
      print(user_number2 - user_number1)


if user_operator == "*":
  print(int(user_number1 * int(user_number2)))
  total_result = int(user_number1 * int(user_number2))

  if user_operator == "/":
      if user_number1 > user_number2:
          print(int(user_number1 / int(user_number2)))
      else:
          print(int(user_number1 / int(user_number2)))
          total_result = int(user_number1 / int(user_number2))


totalresult(user_number1, user_number2)

if total_result > 100:
  print("The result is more than 100.")

  

else:
  print("The result is less than 100.")

NeuroZ
  • 7
  • 1
  • What, *exactly* is the problem? Please see [ask] and the [help]. You haven't actually asked a question here, just dumping the code and asking "what's wrong" isn't a valid quesiton. You need to tell us what's wrong, e.g. the code raises an error, and then provide that error message and a [mcve] that reproduces your problem. Stack overflow is not a private, free, debugging service. – juanpa.arrivillaga Oct 04 '21 at 17:59
  • I am getting this error; Traceback (most recent call last): File "C:\Users\Selmin\PycharmProjects\CodeExperience\Input Game.py", line 29, in total_result = totalresult(user_number1, user_number2) TypeError: totalresult() takes 0 positional arguments but 2 were given – NeuroZ Oct 04 '21 at 18:04
  • `while user_operator == "-":` Will result in an infinite loop because you don't modify `user_operator` in the loop. Same with `while user_number1 > user_number2:` – 001 Oct 04 '21 at 18:04
  • **No**. Again, please read [ask] and the [help]. Don't put this information in a *comment*, put it in the *question itself as formatted text*. And make sure you are providing a [mcve], don't just dump your full code here. – juanpa.arrivillaga Oct 04 '21 at 18:05
  • 1
    The message explains it pretty well. You are calling a function and passing 2 arguments when the function expects 0 arguments. – 001 Oct 04 '21 at 18:06
  • I also put it on the post, i looked at the code for a while but i just cant find out whats wrong. I would really appreciate it to at least know at what part it goes wrong or what i can do different? – NeuroZ Oct 04 '21 at 18:07
  • In your own words, where the code says `totalresult(user_number1, user_number2)`, what do you think this means? Where the error message says `totalresult() takes 0 positional arguments but 2 were given`, what do you think `argument` means? Notice how, between the brackets for `totalresult(user_number1, user_number2)`, we see `user_number1` and `user_number2`? That's 2 things, right? Do you see how this might relate to the error? Notice how, between the brackets for `def totalresult():`, there isn't anything, right? That's 0 things, right? Do you see how this might relate to the error? – Karl Knechtel Aug 15 '22 at 05:18
  • In your own words, when you write `totalresult(user_number1, user_number2)`, what is the *intended purpose* of putting the `user_number1, user_number2` part there? It's so the function can use those values, right? Do you suppose the function should be written in a way that says that it will *expect* those values? – Karl Knechtel Aug 15 '22 at 05:19

2 Answers2

0

You re calling a function with parameters which dont have any parameters. You have this:

def totalresult():

You should have this:

def totalresult(user_number1, user_number2):
João Melo
  • 42
  • 4
  • if total_result > int(100): TypeError: '>' not supported between instances of 'NoneType' and 'int' – NeuroZ Oct 04 '21 at 18:26
  • @NeuroZ One question per post, please. Please read [ask] and note well that this is **not a discussion forum** nor a help desk. And please try to study a tutorial for Python from start to finish. There are no shortcuts to the fundamentals. – Karl Knechtel Aug 15 '22 at 05:19
-1

As the error message indicates:

 line 29, in total_result = totalresult(user_number1, user_number2) TypeError: totalresult() takes 0 positional arguments but 2 were given

The function, totalresult(), you defined takes no arguments, but later when you call the function, you provide two arguments, e.g. totalresult(user_number1, user_number2), which caused the problem.

As a quick fix to this, you will have to add two arguments to your function definition like so:

def totalresult(user_number1, user_number2):
   ...
Harry
  • 1,147
  • 13
  • 13
  • i got this error; if total_result > int(100): TypeError: '>' not supported between instances of 'NoneType' and 'int' – NeuroZ Oct 04 '21 at 18:25
  • Go back and fix the function def first as suggested; otherwise, this line is not valid: total_result = totalresult(user_number1, user_number2) – Harry Oct 04 '21 at 18:29
  • i fixed the def and added the user_number 1 and 2 inside, tried to run the problem and after that i got this last problem – NeuroZ Oct 04 '21 at 18:33
  • 1
    @NeuroZ Because the function does not `return` a value. [How is returning the output of a function different from printing it?](https://stackoverflow.com/a/750154) – 001 Oct 04 '21 at 18:34
  • How is the "user_operator" being past to the function? You may need to provide a third argument to the function definition. – Harry Oct 04 '21 at 18:37
  • oh my god thanks so much everything fixed itself when i used return instead of printing – NeuroZ Oct 04 '21 at 18:41