OSG
OSG

Reputation: 67

Python script works when I run from IDLE but not in command line?

I'm working on a school project, and we have to make a script. Basically, whenever I'm in IDLE and I press F5 to run the script everything works fine, but when I double click the python file itself and go into command line, I only get to input one thing (R or L) and then the window closes. Why is this ? I think one of the requirements for the assignment is that it need to be executed in Command Line, so this is why I'm asking. Here is my script (it's not finished yet)

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()

Upvotes: 2

Views: 7399

Answers (2)

Dalvenjia
Dalvenjia

Reputation: 2033

You can run it by double clicking if the file extension is associated to the python interpreter, the problem is with the terminal/cmd window closing immediately after the script finish execution. to fix this you have 2 options:

  1. Add a blocking statement to your script at the end, for example a input('Press enter to exit.'), after your main() execution. This way the script will not terminate immediately but after you press a key.

  2. Run the your script from a terminal/cmd instance that will remain after the script execution

Upvotes: 0

Irmen de Jong
Irmen de Jong

Reputation: 2847

I'm assuming you're on Windows?

This is the way window's console is working; it closes as soon as the program running in it exits. I'm guessing your program ends with an error or just ends normally, in either case, windows immediately closes the console window (yay).

One way to avoid this is adding a input("press enter to exit.") at the end of your program. This doesn't help if the program crashes though, in which case the comment above by Vinny is the way to go: open a command window manually, and manually launch your program by typing python and then the path to your .py file. IN case of an error in your program it will then get printed and you'll drop back into the cmd shell prompt.

Upvotes: 1

Related Questions