首页 > 解决方案 > 程序在Pycharm和IDLE中运行,直接打开就崩溃

问题描述

我最近开始学习python,我玩得很开心。

我编写了以下程序,它在 IDLE 和 Pycharm 中完美运行,但是当我尝试从资源管理器打开文件时崩溃。它在我发送给朋友的计算机上也是如此。我和我的朋友都在 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) + '!')

我试过重新安装 Python。即使我将代码复制到另一个文档并删除除打印语句之外的所有内容,它仍然会因任何原因而崩溃。我制作的其他程序仍然运行良好,以相同的方式打开并在终端中运行。

伙计们,我完全不知所措。有任何想法吗?

标签: pythonrandompycharm

解决方案


你的程序没有崩溃。由于您通过单击资源管理器中的图标打开它的方式,它成功完成但在您有时间查看输出之前关闭。把类似的东西

input("Press enter to close")

在文件的底部。这input()将使窗口保持打开足够长的时间让您看到输出。


推荐阅读