简体   繁体   English

程序在运行时立即关闭

[英]Program immediately closes when ran

I just finished writing my first Python program however as soon as i go to run it the window closes. 我刚刚完成了我的第一个Python程序的编写,但是一旦我运行它,窗口就会关闭。 I tried a few fixes found in other threads with no luck. 我尝试了其他线程中没有的运筹帷fix。 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..."

You can add "import pdb" in the 1st line, then run and it will help you debug your programme. 您可以在第一行中添加“ import pdb”,然后运行,它将帮助您调试程序。

Several grammar error: 1. Line 7, while loop use ":" instead of ";" 几个语法错误:1.第7行,而while循环使用“:”代替“;” 2. Line 8, IndentationError: expected an indented block 3. Line 18, if op ==1: , and same for all your if statement. 2.第8行,IndentationError:应该缩进块。3.第18行,如果op == 1:,并且对于所有if语句都是相同的。 4. Line 52, print ('Please 4.第52行,打印(“请

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

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. 我假设您是从快捷方式或启动器启动程序的,这就是为什么您不会看到python通常给您的错误回溯的原因。

Instead, run the program in a console. 而是在控制台中运行该程序。 For example, python myprogram.py or python3 myprogram.py . 例如, python myprogram.pypython3 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 所有if / elseif / else /而行应以冒号结尾
  • the string on line 51 should have a starting quote 第51行的字符串应以引号开头
  • everything under the while line (which should end with a colon) should be indented while行下的所有内容(应以冒号结尾)都应缩进
  • (not a syntax error) you should check for rep < cycle , not the other way arround (不是语法错误),您应该检查rep < cycle ,而不是反过来

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM