当前位置: 动力学知识库 > 问答 > 编程问答 >

python 3.4 - lock code (can someone tell me how to improve)

问题描述:

hey I am very new to coding and I have created this relatively simple code to do with my basic knowledge of the language. However I have had to do with some unwanted code including shutting it down if password is too long. so could anybody help me with any improvements to this.

Thanks in advance,

Josh

import time

passwordx = float(input('create your password'))

def password():

passwordx

if passwordx>9999:

print('password is wrong\n innitiating shutdown')

x=5

time.sleep(1)

print(x)

x-=1

time.sleep(1)

print(x)

x-=1

time.sleep(1)

print(x)

x-=1

time.sleep(1)

print(x)

x-=1

time.sleep(1)

print(x)

x-=1

time.sleep(1)

print(x)

x-=1

quit()

else:

print('code saved')

x=1

while x <1000:

print('###############')

x+=1

def login():

passwordguess = float(input('please enter password'))

if passwordguess == passwordx:

print('access granted')

else:

print('wrong')

print('here\'s a clue', (passwordx-passwordguess)**2)

login()

password()

login()

网友答案:

A few general things:

  1. Limit the validity of variables to the absolutely necessary.
  2. Try to limit the dependencies of different variables to each other.
  3. Avoid branches as far as possible.

Architectures Optimization Reference Manual

PythonSpeed

分享给朋友:
您可能感兴趣的文章:
随机阅读: