简体   繁体   English

程序在Pycharm和IDLE下运行,直接打开就崩溃

[英]Program runs in Pycharm and IDLE, but crashes when opened directly

I recently started learning python, and I've been having a lot of fun.我最近开始学习 python,我玩得很开心。

I wrote the below program, which runs perfectly in IDLE and Pycharm, but crashes when I try to open the file from Explorer.我编写了以下程序,它在 IDLE 和 Pycharm 中完美运行,但是当我尝试从资源管理器打开文件时崩溃。 It does the same on the computers of friend's I've sent it to.它在我发送给朋友的计算机上也是如此。 Both me and my friends are on Windows 10.我和我的朋友都在 Windows 10 上。

# This program generates networking technobabble, useful for sounding smart and impressing idiots
# This was made to get a working idea of how to use dictionaries.
# It could easily be made more efficient but doo doo gamer brain no code good

import random

print('Welcome to the Networking Technobabble Generator.')
print('This program generates fake acronyms for imaginary protocols')

# Setting out all the possible words.
# Some repeat across the two dictionaries because there weren't multiple possibilities

dic1 = {'a': 'Address', 'b': 'Bridge', 'c': 'Cloud', 'd': 'Data', 'e': 'Exterior', 'f': 'Forwarding', 'g': 'Gateway',
        'h': 'Host', 'i': 'Interior', 'k': 'Kernel', 'l': 'Local', 'm': 'Media', 'n': 'Network',
        'o': 'Open', 'p': 'Port', 'r': 'Routing', 's': 'Service', 't': 'Trunked', 'u': 'User',
        'v': 'Virtual', 'w': 'Weighted', 'x': 'Xpress', 'st': 'Spanning-Tree', 'ai': 'Artificial Intelligence',
        'lb': 'Load Balancing', 'pp': 'Point-to-Point'}

dic2 = {'a': 'Application', 'b': 'Basic', 'c': 'Class-based', 'd': 'Decentralized', 'e': 'Enhanced', 'f': 'Fail-safe',
        'g': 'Generic', 'h': 'Hop', 'i': 'Integrated', 'k': 'Key', 'l': 'Logical', 'm': 'Multipoint', 'n': 'Nonbroadcast',
        'o': 'Overload', 'p': 'Primary', 'r': 'Routing', 's': 'Service', 't': 'Trunked', 'u': 'User',
        'v': 'Virtual', 'w': 'Weighted', 'x': 'Xpress', 'st': 'Spanning-Tree', 'ai': 'Artificial Intelligence',
        'lb': 'Load Balancing', 'pp': 'Point-to-Point'}

# Everything is written out instead of imported because J, Q, X, Y, and Z don't work well, so we exclude them
# It being a little janky allows new letters to be added easily!
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w',
           'st', 'ai', 'lb', 'pp']

# Generating the letters of our acronym
l1 = random.choice(letters)
l2 = random.choice(letters)

# Ensuring you don't get the same letter for l1 and l2
while l1 == l2:
    l2 = random.choice(letters)
    continue

# Choosing l3, then doing same check as above
l3 = random.choice(letters)
while l3 == l1 or l3 == l2:
    l3 = random.choice(letters)
    continue

# Getting the words from the dictionary. The 'if' statement is to choose which dictionary to use.
# Random-ness is weighted toward the first list since it's the more common terms
whichdic = random.randint(1, 3)
if whichdic == 1 or whichdic == 2:
    word1 = dic1.get(str(l1), 0)
else:
    word1 = dic2.get(str(l1), 0)

whichdic = random.randint(1, 3)
if whichdic == 1 or whichdic == 2:
    word2 = dic1.get(str(l2), 0)
else:
    word2 = dic2.get(str(l2), 0)

whichdic = random.randint(1, 3)
if whichdic == 1 or whichdic == 2:
    word3 = dic1.get(str(l3), 0)
else:
    word3 = dic2.get(str(l3), 0)

# Making the letters uppercase. This would have been avoidable if I had planned ahead
l1 = l1.upper()
l2 = l2.upper()
l3 = l3.upper()

# Printing! :)
print('Your acronym is ' + str(l1) + str(l2) + str(l3) + ', which stands for ' + str(word1) + ' ' + str(word2) + ' '
      + str(word3) + '!')

I've tried reinstalling Python.我试过重新安装 Python。 Even if I copy the code to another document and delete everything but the print statements, it still crashes for whatever reason.即使我将代码复制到另一个文档并删除除打印语句之外的所有内容,它仍然会因任何原因而崩溃。 My other programs I've made still run fine being opened the same way and running in the terminal.我制作的其他程序仍然运行良好,以相同的方式打开并在终端中运行。

I'm completely at a loss guys.伙计们,我完全不知所措。 Any ideas?有任何想法吗?

Your program isn't crashing.你的程序没有崩溃。 Because of the way you are opening it by clicking an icon in Explorer, it finishes successfully but closes before you have time to see the output.由于您通过单击资源管理器中的图标打开它的方式,它成功完成但在您有时间看到 output 之前关闭。 Put something like把类似的东西

input("Press enter to close")

at the bottom of the file.在文件的底部。 The input() will keep the window open long enough for you to see the output. input()将使 window 保持打开足够长的时间,以便您看到 output。

Your program didn't crash, it just terminated before you get a chance to read it.您的程序没有崩溃,它只是在您有机会阅读它之前终止。 You can add a delay time like so:您可以像这样添加延迟时间:

from time import sleep
seconds = 10
sleep(seconds)

Or you can add an input statement so the user can chooses when to end it;)或者您可以添加一个输入语句,以便用户可以选择何时结束它;)

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

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