Python Forum
my coding doesn't work plz help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
my coding doesn't work plz help
#5
(Feb-03-2017, 09:35 AM)wavic Wrote: It is possible to define a function into another function but is pointless in your case. Rid of all these math functions which are doing one thing, to return a calculation result of two values. There is no indentation of elif to if

  if(operation == '+'):
      return num1 + num2
  elif(operation == '-'):
      return num1 - num2
  elif(operation == '*'):
      return num1 * num2
  else:
      return num1 / num2
......

print(calculator())
Also, you call calculator() inside its own code. Use while loop instead.
For now, that's all.

All in all, I'd have a different approach to almost everything.

On the contrary... Using functions is a good idea. You then use a dictionary where the keys are the operator strings and the value is the matching function:
ops={'+':add,'-':subtract,'*':multiply}
and use as:
result=ops[operator](num1,num2)
(python has built-in functions for the base operators (see the operators module), so you can even avoid defining some of the functions)
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Messages In This Thread
my coding doesn't work plz help - by Darbandiman123 - Feb-02-2017, 05:12 PM
RE: my coding doesn't work plz help - by buran - Feb-02-2017, 05:21 PM
RE: my coding doesn't work plz help - by wavic - Feb-03-2017, 09:35 AM
RE: my coding doesn't work plz help - by Ofnuts - Feb-03-2017, 12:51 PM
RE: my coding doesn't work plz help - by wavic - Feb-03-2017, 01:53 PM
RE: my coding doesn't work plz help - by Ofnuts - Feb-03-2017, 05:27 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extending list doesn't work as expected mmhmjanssen 2 348 May-09-2024, 05:39 PM
Last Post: Pedroski55
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 1,073 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 2,061 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 1,015 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 1,836 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 4,528 May-30-2022, 03:31 PM
Last Post: bowlofred
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,169 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,814 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  Process doesn't work but Thread work ! mr_byte31 4 2,783 Oct-18-2021, 06:29 PM
Last Post: mr_byte31
  Psycopg2 doesn't work with python2 MedianykEugene 3 3,098 Aug-10-2021, 07:00 AM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020