首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有人能看一下我的python代码吗?因为我只是个初学者,所以代码很简单

有人能看一下我的python代码吗?因为我只是个初学者,所以代码很简单
EN

Stack Overflow用户
提问于 2019-11-03 05:30:28
回答 1查看 130关注 0票数 0

这是我的代码,当它运行时,它跳过其中一个代码块,它没有显示任何错误或任何东西,它只是跳过我的第一个if条件,并运行其余的条件,这是=

代码语言:javascript
复制
import time

#Cracking PINs

pin = str(input('Type in your 6-digit PIN, it MUST be a number: \n'))
pinLength = len(pin)
solvePin = 0.000002
easyPins = [000000, 0.123456, 0.11111, 0.222222, 0.333333, 0.444444, 0.555555, 0.666666, 0.777777, 0.888888, 0.999999, 0.456123]
pin = int(pin)
pin = pin/1000000

#PIN strength

while(pin in easyPins):
  print('Your PIN is too weak, make another one')
  pin = int(input())

print('Strong PIN!')

#Checking PIN Length and Cracking PIN

startTime = time.time()

if(pinLength == 8):
  confirmPin = int(input('Confirm your PIN \n'))
  confirmPin = confirmPin/1000000
  while(confirmPin != pin):
    print('Not the same PIN\n')
    confirmPin = int(input('Please retype your PIN\n'))
  while(solvePin != confirmPin):
    solvePin += 0.000001
    print(solvePin)
  else:
    while (pinLength != 8):
     int(input('Your PIN is too long or short, type again.\n'))

print(' ')
print('Your Pin is')
print(solvePin)

endTime = time.time()
print(' ')
print ('Elapsed time in seconds', ((endTime - startTime)))
EN

回答 1

Stack Overflow用户

发布于 2019-11-03 05:39:48

您有一行if(pinLength == 8),但是您提示人们输入一个6位数的pin。所以if永远不会是真的。

此外,以下else块的缩进不正确。它没有与if(pinLength == 8)对齐,所以else也永远不会被调用。

代码语言:javascript
复制
else:
  while (pinLength != 8):
    int(input('Your PIN is too long or short, type again.\n'))

因为这是在else中,即使您修复了缩进以便调用它,一旦被调用,您的脚本就会移动到末尾,永远不会经历验证while循环。

实际上我会推荐这样的东西,而不是……

代码语言:javascript
复制
import time

easyPins = ["12345678", "87654321"]
#Cracking PINs
while True: # Keeps looping until broken
    rawpin = input('Type in your 8-digit PIN, it MUST be a number: \n')
    if len(rawpin) != 8:
        print("Pin must be 8 digits! Please try again...")
        continue # Restart at the top of the loop
    # Checks if all characters in the pin are the same, circa https://stackoverflow.com/questions/14320909/efficiently-checking-that-string-consists-of-one-character-in-python
    if rawpin  == len(rawpin ) * rawpin [0]:
        print('Your PIN cannot all be the same digit. Please make another one...')
        continue # Restart at the top of the loop
    if (rawpin in easyPins): 
        print("The pin is too easy. Please try again!")
        continue
    # Use a try/except to validate it's all digits
    # This will error and catch if "pin" can't be converted to an int
    try:  
        pin = int(rawpin)
        # pin = int(rawpin)/1000000 # dont need this, see below
    except: 
        print("Pin must be digits only! Please try again...")
        continue # Restart at the top of the loop
    # Do the validation here
    confirmPin = input('PLease re-type your 8-digit PIN: \n')
    if confirmPin != rawpin:
        "The pins do not match!"
        continue # Try again
    else:
        # Dont need to do checks here, since they were done with rawpin
        confirmPin = int(confirmPin)
        break


print('Strong PIN!')
#Checking PIN Length and Cracking PIN
#===============================================================================
# Unfortunately, this will not work, because of the way python does math. 
# The floats come out to a greater decimal than the original inputs. 
# I.e. ...
# 0.1034239999936388
# 0.1034249999936389
# 0.1034259999936389
# 0.1034269999936389
# 0.103427999993639
# 0.103428999993639
# 0.103429999993639
# 0.103430999993639
# 0.1034319999936391
### Original
# startTime = time.time()
# solvePin = 0.000002
# print("pin=", pin)
# print("confirmPin=", confirmPin)
# while(solvePin != confirmPin):
#     print(solvePin)
#     solvePin += 0.000001
# print(solvePin) # This should be outside the loop




# Instead, just compare the numeric values without the math
# Right now, it doesn't matter if the int(pin) is shorter than 8 digits
# You just need them to be the same number
solvePin = 0
startTime = time.time()
for i in range(1, 99999999): # 99999999 is the maximum value of any pin
    solvePin += 1
    if solvePin == pin:
        # leading zeros would be removed when the pin is made into an int
        # However, they will always ONLY be leading zeros that are missing
        # So just pad the string out if needed.
        strpin = str(solvePin)
        while len(strpin) < 8:
            strpin = "0" + strpin
        break
print() # Dont need the ' '
print('Your Pin is:', strpin)

endTime = time.time()
print()
print ('Elapsed time in seconds', ((endTime - startTime)))

输出:

代码语言:javascript
复制
Type in your 8-digit PIN, it MUST be a number: 
00123123
PLease re-type your 8-digit PIN: 
00123123
Strong PIN!

Your Pin is: 00123123

Elapsed time in seconds 0.04227638244628906
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58675477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档