程序在运行时立即关闭

时间:2014-07-14 23:34:47

标签: python python-2.7

我刚刚编写了我的第一个Python程序,但是一旦我运行它,窗口关闭。我在其他线程中尝试了一些没有运气的修复程序。这是代码,语法中的某些内容会导致错误输出吗?谢谢你的帮助!

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

2 个答案:

答案 0 :(得分:1)

您可以添加&#34;导入pdb&#34;在第一行,然后运行,它将帮助您调试您的程序。

几个语法错误: 1.第7行,while循环使用&#34;:&#34;而不是&#34;;&#34; 2.第8行,IndentationError:预期有一个缩进块 3.第18行,如果op == 1:,则对所有if语句都相同。 第52行,打印(&#39;请

完成。

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

答案 1 :(得分:0)

您的代码中存在一些语法错误。

我假设你是从快捷方式或启动器启动程序,这就是为什么你不会看到python通常给你的错误跟踪。

而是在控制台中运行该程序。例如,python myprogram.pypython3 myprogram.py。这应该会给你一个追踪程序错误的追溯。

代码中的错误:

  • 所有if / elseif / else / while行应以冒号结尾
  • 第51行的字符串应该有一个起始引用
  • while行下面的所有内容(应以冒号结尾)应缩进
  • (不是语法错误)您应该检查rep < cycle,而不是其他方式arround

这是固定版本:

#!/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..."