流程图
尝试创建密码生成器
无法使if语句正确工作,也不知道这是否真的是解决问题的正确方法。我需要它将每个字母的数字表示除以3,如果它是一个整数,则返回一个#。
password = input("password: ")
password = password.lower()
output = []
for character in password:
number = ord(character) - 96
output.append(number)
x = output
if x / 3:
print ("#")
print (output)
我收到以下错误:TypeError:只能将列表(而不是"int"(连接到列表
我不知道你想用可被3整除的数字做什么。为了让您开始,这里有一个示例代码。看看这是否有助于你朝着正确的方向开始。
password = input('enter password :').lower()
output = []
for c in password:
num = ord(c) - 96
output.append(num)
all_div_by_3 = True
for i in output:
if i%3 != 0: #checks if remainder of i/3 is zero. if zero, then divisible, else not divisible.
all_div_by_3 = False
break
if all_div_by_3: #is same as if all_div_by_3 == True:
print ('all divisible by 3')
else:
print ('all characters are not divisible by 3')
其输出如下:
enter password :cliff
all divisible by 3
enter password :rock
all characters are not divisible by 3
经过大量阅读和研究,很明显我需要使用if、elif和else函数。以下是已完成的项目。
password = input("password: ")
password = password.lower()
output = []
for character in password:
number = ord(character) - 96
output.append(number)
for i in output:
if(i% 3 == 0) :
print('#', end ="")
elif(i% 5 == 0) :
print('%', end ="")
else:
print(chr(i+98), end="")