Python Forum
1st Script written doesn't output anything. Need some else's eyes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
1st Script written doesn't output anything. Need some else's eyes
#1
Hi All,
I am 24 hours into Python37 coding using IDLE and have written a calculator script. The calc script should request 2 variables and what needs done with them +-*/. Nothing shows when I run it in Python, it doesn"t ask any of the questions. Script is written off a youtube lesson. Any help would be appreciated. Sorry, when I edit the text all the indents are there and when I save it they all disappear. Any reason for this?

#returns the sum of num1 and num2
def add(num1, num2):
    return num1 + num2

#returns sum of subtracting num1 and num2
def sub(num1, num2):
    return num1 - num2

#returns sum of num1 * num2
def mul(num1, num2):
    return num1 * num2

#returns sum of num1 / num2
def div(num1, num2):
    return num1 / num2
    

def main(): #Shouldn't there be something between the brackets?(easier than writing parentisis)
    operation = input("What do you want to do (+,-,*,/):")
    if (operation != '+' and operation != '-' and operation != '*' and operation != '/'):
        #invalid operation
        print('You must enter a valid operation')
    else:
        var1 = int(input("Enter number1:"))
        var2 = int(input("Enter number2:"))
        if(operation == '+'):
            print(add(var1,var2))
        elif(operation == '-'):
            print(sub(var1 - var2))
        elif(operation =='*'):
            print(mul(var1 * var2))
        else:
            print(div(var1 / var2))
Reply
#2
In your "main" function, you need to change you're elif's to look like the "if" statement, you can also remove all the redundant parenthesis:

        if operation == '+':
            print(add(var1, var2))
        elif operation == '-':
            print(sub(var1, var2))
        elif operation == '*':
            print(mul(var1, var2))
        else:
            print(div(var1, var2))
Then you need to call the function "main()". Add this to the end of your script:

if __name__ == "__main__":
    main()
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
@sparkz_alot Thank you for the help. While I was frustrated over this problem, my mind kept telling me "There is a better way to do this" Am I imagine this or is the code a little lunky? Perhaps a little cleaner method?

Is there any real reason to def main() at all?
Reply
#4
(Feb-20-2018, 01:29 AM)Kym Wrote: Is there any real reason to def main() at all?
So your file can safely be imported from a different file. Which is useful for testing, or if you're building a library.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to crop eyes/nose/mouth in the face only? [OpenCV] buzzdarkyear 0 2,159 Jan-11-2022, 01:41 PM
Last Post: buzzdarkyear
  Real-Time output of server script on a client script. throwaway34 2 2,061 Oct-03-2021, 09:37 AM
Last Post: ibreeden
  string function doesn't work in script ClockPillow 3 2,420 Jul-13-2021, 02:47 PM
Last Post: deanhystad
  Python script to summarize excel tables, then output a composite table? i'm a total n surfer349 1 2,357 Feb-05-2021, 04:37 PM
Last Post: nilamo
  Curses script doesn't work wavic 1 4,158 Jan-08-2021, 09:11 PM
Last Post: wavic
  Import output from python script to another varietyjones 1 1,911 Oct-12-2020, 09:07 PM
Last Post: bowlofred
  Reading from the written output buduboti 0 1,196 Apr-25-2020, 09:17 PM
Last Post: buduboti
  Manipulating the filename of an output script mckinneycm 4 11,898 Jan-15-2020, 07:29 PM
Last Post: mckinneycm
  Python Script Spawned by Cron or Systemd doesn't write files..? johnnyrobot 2 2,589 May-24-2019, 07:04 PM
Last Post: Larz60+
  Redircet output of Python script to .txt file gurbhej_singh 7 8,441 May-20-2019, 01:11 AM
Last Post: woooee

Forum Jump:

User Panel Messages

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