【问题标题】:Python script works when I run from IDLE but not in command line?当我从 IDLE 运行但不在命令行中运行时,Python 脚本有效吗?
【发布时间】:2017-10-19 04:08:42
【问题描述】:

我正在做一个学校项目,我们必须编写一个脚本。 基本上,每当我处于空闲状态并按 F5 运行脚本时,一切正常,但是当我双击 python 文件本身并进入命令行时,我只能输入一个东西(R 或 L),然后窗口关闭。为什么是这样 ?我认为分配的要求之一是它需要在命令行中执行,所以这就是我问的原因。 这是我的脚本(还没写完)

import os
import csv
import sys


def main():
    menu()



def menu():
    print ("******MAIN MENU******")
    print()
    veryFirstChoice = input("Would you like to register an account, or log in? Type R to register and L to log in ").upper()
    if veryFirstChoice == "R":
        register()
    elif veryFirstChoice == "L":
        login()

def login():
    username = input("Please enter your username")
    password = input("Please enter your password")  
    for line in open("accountfile.txt","r").readlines():
        login_info = line.split()
        if username == login_info[0] and password == login_info[1]:
            print("Correct credentials!")
            return quiz()
    print("Incorrect credentials,please try again. ")
    return login()

def register():
    username = input("Please input the first 3 letters of your first name and your age ")
    password = input("Please input your desired password ")
    file = open("accountfile.txt","a")
    file.write(username)
    file.write(" ")
    file.write(password)
    file.write("\n")
    file.close()
    menu()

def quiz():
    choice1 = input ("""Would you like to take the Chemistry Quiz,or the Computer Science quiz ? Type in CH for Chemistry or CS for Computer Science """).upper()
    score = 0

    if choice1 == "CH":
        choice2 = input ("Would you like to do the easy, medium or hard questions ?").lower()
        if choice2 == "easy":
            load_profile = open("chemistryquiz.txt","r")
            lines = load_profile.read().splitlines()
            question1 = lines[4]
            question2 = lines[5]
            question3 = lines[6]
            question4 = lines[7]
            question5 = lines[8]
            print (question1)
            answer1 = input("Please enter your answer ").upper()
            if answer1 != "A":
                print ("You got the wrong answer, your score is", score)
            else:
                score1 = (score+1)
                print ("Your score is", score1)
            print (question2)
            answer2 = input("Please enter your answer ").upper()
            if answer2 != "A":
                score1 = score1
                print ("You got the wrong answer, your score is", score1)

            else:
                score2 = (score1+1)
                print ("Your score is", score2)
            print (question3)
            answer3 = input("Please enter your answer ").upper()
            score2 = score1
            if answer3 != "A":
                score3 = score2
                print ("You got the wrong answer, your score is", score3)
            else:
                score3 = (score2+1)
                print ("Your score is", score3)
            print (question4)
            answer4 = input("Please enter your answer ").upper()
            if answer4 != "A":
                score4 = score3
                print ("Wrong answer, your score is ", score4)
            else:
                score4 = (score3+1)
                print ("Your score is", score4)
            print (question5)
            answer5 = input("Please enter your answer ").upper()
            if answer5 != "A":
                score5 = score4
                print ("You got the wrong answer, your score is ", score5)
            else:
                score5 = (score4+1)
                print ("Well done, your score is ", score5)
        if choice2 == "medium":
            load_profile = open("chemistryquiz.txt","r")
            lines = load_profile.read().splitlines()
            question1 = lines[13]
            question2 = lines[14]
            print (question1)
            answer1 = input("Please enter answer ").upper()
            if answer1 != "A":
                score1 = score
                print ("Wrong answer ", score1)
            else:
                score1 = (score+1)
                print ("Noice, score is ", score1)

main()

【问题讨论】:

  • Python 脚本是从命令行执行的,而不是通过双击它们。打开cmd.exe,执行python path/to/your/script.py

标签: python


【解决方案1】:

我假设您使用的是 Windows?

这是窗口控制台的工作方式;一旦在其中运行的程序退出,它就会关闭。我猜您的程序以错误结束或只是正常结束,在任何一种情况下,Windows 都会立即关闭控制台窗口(耶)。

避免这种情况的一种方法是在程序末尾添加input("press enter to exit.")。但是,如果程序崩溃,这将无济于事,在这种情况下,Vinny 上面的评论是可行的方法:手动打开命令窗口,然后通过键入 python 手动启动程序,然后输入 .py 文件的路径.如果您的程序出现错误,它将被打印出来,您将返回到 cmd shell 提示符。

【讨论】:

    【解决方案2】:

    如果文件扩展名与python解释器相关联,您可以通过双击运行它,问题是终端/cmd窗口在脚本完成执行后立即关闭。要解决此问题,您有 2 个选项:

    1. 在执行main() 之后,在脚本末尾添加一个阻塞语句,例如input('Press enter to exit.')。这样脚本不会立即终止,而是在您按下一个键之后。

    2. 从终端/cmd 实例运行脚本,脚本执行后将保留该实例

    【讨论】:

    • 我相信input 只会在新行结束,所以“any key”应该是“enter”。
    • 我在相同的上下文中有一个不同的问题 - 我正在尝试运行线程。这些程序在空闲时工作正常,但无法通过 python 启动器启动。各种各样的程序——从简单的线程到 tkinter GUI。我正在使用 Windows 10 和 Python 3.8
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    相关资源
    最近更新 更多