0

I just finished writing my first Python program however as soon as i go to run it the window closes. I tried a few fixes found in other threads with no luck. Here is the code, is something in the syntax making it error out? Thanks for the help!

print ('Lets do some math nerd!')
print ('How many problems would you like to solve?')
cycle = int(input())
rep = 0
while cycle < rep;
rep = rep + 1;

print ('What operation would you like to perform?')
print('Press 1 for Addition')
print('Press 2 for Subtraction')
print('Press 3 for Multiplication')
print('Press 4 for Division')

op = int(input())

if op ==1
    print ('Enter your first number.')
    num1 = int(input())
    print ('Enter your second number.')
    num2 = int(input())
    result = (num1 + num2)
    print result

elif op ==2
    print ('Enter your first number.')
    num1 = int(input())
    print ('Enter your second number.')
    num2 = int(input())
    result = (num1 - num2)
    print result

elif op ==3
    print ('Enter your first number.')
    num1 = int(input())
    print ('Enter your second number.')
    num2 = int(input())
    result = (num1 * num2)
    print result

elif op ==4
    print ('Enter your first number.')
    num1 = int(input())
    print ('Enter your second number.')
    num2 = int(input())
    result = (num1 / num2)
    print result

else    
    print ('ERROR')
    print (Please select a number 1 through 4.')
print ('Thanks for using my calculator!')

closeInput = raw_input("Press ENTER to exit")
print "Closing..."
Axios_Andrew
  • 63
  • 1
  • 2
  • 6

2 Answers2

1

You can add "import pdb" in the 1st line, then run and it will help you debug your programme.

Several grammar error: 1. Line 7, while loop use ":" instead of ";" 2. Line 8, IndentationError: expected an indented block 3. Line 18, if op ==1: , and same for all your if statement. 4. Line 52, print ('Please

Done.

import pdb

print ('Lets do some math nerd!')
print ('How many problems would you like to solve?')
cycle = int(input())
rep = 0
while cycle < rep:
        rep = rep + 1

print ('What operation would you like to perform?')
print('Press 1 for Addition')
print('Press 2 for Subtraction')
print('Press 3 for Multiplication')
print('Press 4 for Division')

op = int(input())

if op ==1:
    print ('Enter your first number.')
    num1 = int(input())
    print ('Enter your second number.')
    num2 = int(input())
    result = (num1 + num2)
    print result

elif op ==2:
    print ('Enter your first number.')
    num1 = int(input())
    print ('Enter your second number.')
    num2 = int(input())
    result = (num1 - num2)
    print result

elif op ==3:
    print ('Enter your first number.')
    num1 = int(input())
    print ('Enter your second number.')
    num2 = int(input())
    result = (num1 * num2)
    print result

elif op ==4:
    print ('Enter your first number.')
    num1 = int(input())
    print ('Enter your second number.')
    num2 = int(input())
    result = (num1 / num2)
    print result

else:
    print ('ERROR')
    print ('Please select a number 1 through 4.')
print ('Thanks for using my calculator!')

closeInput = raw_input("Press ENTER to exit")
print "Closing..."
gary
  • 1,409
  • 2
  • 16
  • 27
0

You have a few syntax errors in your code.

I assume you are launching the program from a shortcut or launcher, which is why you would not see the error traceback python normally gives you.

Instead, run the program in a console. For example, python myprogram.py or python3 myprogram.py. This should give you a traceback for the error that kills the program.

The errors in your code:

  • all if/ elseif/ else/ while lines should end with a colon
  • the string on line 51 should have a starting quote
  • everything under the while line (which should end with a colon) should be indented
  • (not a syntax error) you should check for rep < cycle, not the other way arround

This is the fixed version:

#!/usr/bin/python
print ('Lets do some math nerd!')
print ('How many problems would you like to solve?')
cycle = int(input())
rep = 0
while rep < cycle:
    rep = rep + 1

    print ('What operation would you like to perform?')
    print('Press 1 for Addition')
    print('Press 2 for Subtraction')
    print('Press 3 for Multiplication')
    print('Press 4 for Division')

    op = int(input())

    if op ==1:
        print ('Enter your first number.')
        num1 = int(input())
        print ('Enter your second number.')
        num2 = int(input())
        result = (num1 + num2)
        print result

    elif op ==2:
        print ('Enter your first number.')
        num1 = int(input())
        print ('Enter your second number.')
        num2 = int(input())
        result = (num1 - num2)
        print result

    elif op ==3:
        print ('Enter your first number.')
        num1 = int(input())
        print ('Enter your second number.')
        num2 = int(input())
        result = (num1 * num2)
        print result

    elif op ==4:
        print ('Enter your first number.')
        num1 = int(input())
        print ('Enter your second number.')
        num2 = int(input())
        result = (num1 / num2)
        print result

    else:
        print ('ERROR')
        print ('Please select a number 1 through 4.')

print ('Thanks for using my calculator!')

closeInput = raw_input("Press ENTER to exit")
print "Closing..."
Oleg
  • 1,471
  • 9
  • 20
  • Thank you, it seems to be working now although i still cant get it to run from console, i think i need to do more troubleshooting with that. – Axios_Andrew Jul 14 '14 at 23:54
  • What operating system are you using, and what is the command you use to run it? What does the console say when you enter the command? – Oleg Jul 14 '14 at 23:56
  • I am on win 7, when i am on command line i can get python to run by typing C:\python27\python exe, however if i just type python it will not run giving me an error that it is not defined. – Axios_Andrew Jul 14 '14 at 23:58
  • What happens when you run `C:\python27\python.exe myprogram.py` (where myprogram.py is your program)? You can get the `python` command by [adding python to your path](http://stackoverflow.com/questions/6318156/adding-python-path-on-windows-7) – Oleg Jul 15 '14 at 00:02
  • I will look into that link a bit later when i have some more time. Thank you for your help Oleg. – Axios_Andrew Jul 15 '14 at 00:07