【问题标题】:Having trouble running a python program on CMD on windows在 Windows 上的 CMD 上运行 python 程序时遇到问题
【发布时间】:2014-01-19 00:45:53
【问题描述】:

这是程序。它在 IDLE 上运行良好,但在询问您是否知道密码长度后崩溃。我似乎无法弄清楚想要我失踪。我很乐意提供任何帮助。

import itertools
import string
import sys, os, cmd

from datetime import datetime
FMT = '%Y-%m-%d %H:%M:%S'
passwordstried = 0


numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0,]
#symbols = [
lowercaseletters =  ["q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","g","h","j","k","l","z","x","c","v","b","n","m"]
uppercaseletters = ["Q","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","G","H","J","K","L","Z","X","C","V","B","N","M"]


stuff = lowercaseletters + uppercaseletters + numbers

if (input("Do you have the length of the password?") == 'y'):
    lengthstartingvalue = int(input("Password length: "))
else:
    lengthstartingvalue = 0


starttime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(starttime)





starttime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
for L in range(lengthstartingvalue, len(stuff)+1):
    for subset in itertools.combinations_with_replacement(stuff, L):
        print(subset)
        passwordstried = passwordstried + 1
    if (L>lengthstartingvalue-1):
        break

endtime = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
elapsed = datetime.strptime(endtime, FMT) - datetime.strptime(starttime, FMT)
print ('Time elapsed:',elapsed)
print ('Passwords tried:',passwordstried)

【问题讨论】:

  • 它是如何崩溃的?你能发布例外吗?
  • 就是这样。我什至看不到发生了什么。
  • 您能提供更多信息吗?追溯?看起来您正在使用 Python 3.x。问题是窗户消失了吗?如果是这样,只需添加 input() 作为最后一条语句,以便窗口保持打开状态,直到您点击
  • @Conner 你能说明一下你使用的是哪个版本的 Python 吗?
  • @Levon 感谢您的慷慨,但您的回答效果很好。 +1 给你。

标签: python windows python-3.x cmd


【解决方案1】:

@275365 是对的,你应该使用

if (raw_input("Do you have the length of the password?") == 'y'):

而不是

if (input("Do you have the length of the password?") == 'y'):

使用input() 会导致崩溃,

In [11]: run tt.py
Do you have the length of the password?y
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
C:\Program Files (x86)\ipython-0.12.1\IPython\utils\py3compat.py in execfile(fname, glob, loc)
    166             else:
    167                 filename = fname
--> 168             exec compile(scripttext, filename, 'exec') in glob, loc
    169     else:
    170         def execfile(fname, *where):

D:\Users\sp\Desktop\tt.py in <module>()
     16 stuff = lowercaseletters + uppercaseletters + numbers
     17
---> 18 if (input("Do you have the length of the password?") == 'y'):
     19     lengthstartingvalue = int(input("Password length: "))
     20 else:

D:\Users\sp\Desktop\<string> in <module>()

NameError: name 'y' is not defined

在将其更改为 raw_input() 时运行完成而不会崩溃。

【讨论】:

  • OP 声称使用的是 Python 3.3。除非 OP 没有意识到多个 Python 版本存在问题(可能是这种情况),否则这不是答案。
  • 使用 print() 表明我使用的是 3.3 我刚刚卸载了 2.7 以确保不是这个。
  • @Conner 如果将input() 更改为raw_input() 会发生什么?我在 3.2.3(我现在可以访问的唯一 3.x 版本)下运行了这个,没有崩溃。
【解决方案2】:

看来您运行的 IDLE 版本可能与您正在编码的版本不同。如果我理解正确,raw_input() 在您的 CMD 上工作但在 IDLE 中中断,而 input() 在 IDLE 中工作并在 CMD 中中断。您可能需要下载适用于 Python 3 的正确版本的 IDLE,或者如果您拥有它,您只是访问适用于 Python 2 的 IDLE。

否则,可能是您在 Windows 中的 Path 变量有问题。在系统 -> 高级系统设置 -> 环境变量 -> 路径 - 需要设置为您的 Python 3.3 安装。

对我来说,您的路径似乎仍然停留在以前的安装上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多