Home > Net >  Python Code doesnt run, cant see the problem. explained below
Python Code doesnt run, cant see the problem. explained below

Time:10-05

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.")

CodePudding user response:

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):

CodePudding user response:

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):
   ...
  • Related