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
#1
so basically i am trying to make a piece of coding that asks what the person wants to do. however as you can see if you try my coding whenevr it says what do you want to do, no matter what the answer, it does calculator(). please help me. p.s. i havent yet added the coding for battleships() and tictactoe(). thanks

def calculator():
#returns the result of adding num1 + num2
    def add(num1, num2):
        return num1 + num2

#returns the result of subtracting num1 - num2
    def minus(num1, num2):
        return num1 - num2

#returns the result of mutipliying num1 * num2
    def multiply(num1, num2):
        return num1 * num2

#returns the result of dividing num1 / num2
    def divide(num1, num2):
        return num1 / num2



    operation = input("what do you want to do (+,-,*,/): ")
    if(operation != '+' and operation != '-' and operation != '*' and operation != '/'):
        #invalid operation
        print("please enter a vaild operation")
        calculator()
    else:
        num1 = int(input("Enter num1: "))
        num2 = int(input("Enter num2: "))
        if(operation == '+'):
            print(add(num1, num2))
            whattodo()
        elif(operation == '-'):
            print(minus(num1, num2))
            whattodo()
        elif(operation == '*'):
            print(multiply(num1, num2))
            whattodo()
        else:
            print(divide(num1, num2))
            whattodo()

def helloeveryone():

    useless = input('Hello, how are you?')
    myname = input("What is your name? ")
    print ('Nice to meet you, ' + myname)
    myage = input("What's your age, " + myname + "?")  #finds out name and age
    
def whattodo():
    function = input('So what do you want to do? Enter A for calculator, Enter B for Battleships and Enter C for tic tac toe. ')

    if(function == 'Access secret files'):
        secretfiles()
    elif (function == 'a' or 'A'):
        calculator()
    elif (function == 'b' or 'B'):
        battleships()
    elif (function == 'c' or 'C'):
        tictactoe()
    else:
        print ('no') 
            
def secretfiles():    
         print ('lol')       

helloeveryone()
whattodo()
Reply
#2
read this
https://python-forum.io/Thread-Multiple-...or-keyword

there are also other problems, but first fix this one.
Reply
#3
ok done that :). what else is wrong
Reply
#4
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#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
#6
I am using this when I have many, many functions.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
(Feb-03-2017, 01:53 PM)wavic Wrote: I am using this when I have many, many functions.

You should start before that, for two reasons:

  1. Code complexity (and test coverage) goes with the number of ifs...
  2. One day you add a bunch of new functions and have to rewrite your code
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 938 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,786 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  color code doesn't work harryvl 1 889 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  client.get_all_tickers() Doesn't work gerald 2 1,715 Jun-16-2022, 07:59 AM
Last Post: gerald
  pip doesn't work after Python upgrade Pavel_47 10 4,206 May-30-2022, 03:31 PM
Last Post: bowlofred
  For Loop Works Fine But Append For Pandas Doesn't Work knight2000 2 2,014 Dec-18-2021, 02:38 AM
Last Post: knight2000
  Class Method to Calculate Age Doesn't Work gdbengo 1 1,705 Oct-30-2021, 11:20 PM
Last Post: Yoriz
  Process doesn't work but Thread work ! mr_byte31 4 2,620 Oct-18-2021, 06:29 PM
Last Post: mr_byte31
  Psycopg2 doesn't work with python2 MedianykEugene 3 2,957 Aug-10-2021, 07:00 AM
Last Post: ndc85430
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 5,827 Jul-17-2021, 04:19 PM
Last Post: bill_z

Forum Jump:

User Panel Messages

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