【问题标题】:Program runs in Pycharm and IDLE, but crashes when opened directly程序在Pycharm和IDLE中运行,直接打开就崩溃
【发布时间】:2020-09-09 17:59:32
【问题描述】:

我最近开始学习 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。即使我将代码复制到另一个文档并删除除打印语句之外的所有内容,它仍然会因任何原因而崩溃。我制作的其他程序仍然运行良好,以相同的方式打开并在终端中运行。

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

【问题讨论】:

  • 请提供您看到的崩溃

标签: python random pycharm


【解决方案1】:

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

input("Press enter to close")

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

【讨论】:

    【解决方案2】:

    您的程序没有崩溃,它只是在您有机会阅读它之前就终止了。您可以像这样添加延迟时间:

    from time import sleep
    seconds = 10
    sleep(seconds)
    

    或者您可以添加一个输入语句,以便用户可以选择何时结束它;)

    【讨论】:

      猜你喜欢
      • 2022-11-18
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      相关资源
      最近更新 更多