If/else语句根据随机数不工作(Python)
我正在制作一个文本加密程序,它将使用的符号模式(abcd)随机化,但是在第一个if语句之后,程序就不再工作了。新、旧、get和tag是4个字母到符号的翻译。我只测试了第一个if语句,它运行得非常好。此外,任何关于如何缩短代码的提示都将不胜感激If/else语句根据随机数不工作(Python),python,python-3.x,random,Python,Python 3.x,Random,我正在制作一个文本加密程序,它将使用的符号模式(abcd)随机化,但是在第一个if语句之后,程序就不再工作了。新、旧、get和tag是4个字母到符号的翻译。我只测试了第一个if语句,它运行得非常好。此外,任何关于如何缩短代码的提示都将不胜感激 our_str = input("Mornin' ") length = (len(our_str)/2) rounded = round(length) import random rand = random.randint(1,6) print(ran
our_str = input("Mornin' ")
length = (len(our_str)/2)
rounded = round(length)
import random
rand = random.randint(1,6)
print(rand)
if rand <= 2: #abcd
new_str = our_str[:rounded].replace('a', ']')
new1_str = new_str.replace('b', '◙')
new2_str = new1_str.replace('c', '♂')
new3_str = new2_str.replace('d', 'd')
new4_str = new3_str.replace('e', 'e')
new5_str = new4_str.replace('f', '▓')
new6_str = new5_str.replace('g', '─')
new7_str = new6_str.replace('h', 'Φ')
new8_str = new7_str.replace('i', 'Θ')
new9_str = new8_str.replace('j', '≥')
new10_str = new9_str.replace('k', '≤')
new11_str = new10_str.replace('l', 'L')
new12_str = new11_str.replace('m', 'M')
new13_str = new12_str.replace('n', 'V')
new14_str = new13_str.replace('o', 'W')
new15_str = new14_str.replace('p', '►')
new16_str = new15_str.replace('q', '◄')
new17_str = new16_str.replace('r', '→')
new18_str = new17_str.replace('s', '←')
new19_str = new18_str.replace('t', 't')
new20_str = new19_str.replace('u', 'u')
new21_str = new20_str.replace('v', '~')
new22_str = new21_str.replace('w', '⌂')
new23_str = new22_str.replace('x', '°')
new24_str = new23_str.replace('y', '∙')
new25_str = new24_str.replace('z', '☻')
new26_str = new25_str.replace(' ', 'f')
old_str = our_str[rounded:2 * rounded].replace('a', 'Y')
old1_str = old_str.replace('b', 'É')
old2_str = old1_str.replace('c', 'Θ')
old3_str = old2_str.replace('d', '╩')
old4_str = old3_str.replace('e', 'b')
old5_str = old4_str.replace('f', '█')
old6_str = old5_str.replace('g', '=')
old7_str = old6_str.replace('h', '↑')
old8_str = old7_str.replace('i', 'U')
old9_str = old8_str.replace('j', '⌂')
old10_str = old9_str.replace('k', '┬')
old11_str = old10_str.replace('l', 'æ')
old12_str = old11_str.replace('m', '◄')
old13_str = old12_str.replace('n', '1')
old14_str = old13_str.replace('o', 'B')
old15_str = old14_str.replace('p', 'Ñ')
old16_str = old15_str.replace('q', '╡')
old17_str = old16_str.replace('r', '▌')
old18_str = old17_str.replace('s', '♣')
old19_str = old18_str.replace('t', 'Γ')
old20_str = old19_str.replace('u', 'τ')
old21_str = old20_str.replace('v', '╔')
old22_str = old21_str.replace('w', '░')
old23_str = old22_str.replace('x', '┘')
old24_str = old23_str.replace('y', 'ó')
old25_str = old24_str.replace('z', ';')
old26_str = old25_str.replace(' ', 'ß')
get_str = our_str[2 * rounded:3 * rounded].replace('a', 'ç')
get1_str = get_str.replace('b', '1')
get2_str = get1_str.replace('c', '♣')
get3_str = get2_str.replace('d', 'ⁿ')
get4_str = get3_str.replace('e', '♠')
get5_str = get4_str.replace('f', 'S')
get6_str = get5_str.replace('g', '°')
get7_str = get6_str.replace('h', '♫')
get8_str = get7_str.replace('i', 'D')
get9_str = get8_str.replace('j', '│')
get10_str = get9_str.replace('k', '←')
get11_str = get10_str.replace('l', 'ƒ')
get12_str = get11_str.replace('m', 'Æ')
get13_str = get12_str.replace('n', 'P')
get14_str = get13_str.replace('o', '#') #skipped 0781, 6406
get15_str = get14_str.replace('p', '.') #0899
get16_str = get15_str.replace('q', '┤') #0348
get17_str = get16_str.replace('r', 'µ')
get18_str = get17_str.replace('s', 'E')
get19_str = get19_str.replace('t', '§')
get20_str = get20_str.replace('u', '▬')
get21_str = get21_str.replace('v', 'û')
get22_str = get22_str.replace('w', '♀')
get23_str = get23_str.replace('x', '&')
get24_str = get24_str.replace('y', '≈')
get25_str = get25_str.replace('z', 'ª') #4460
get26_str = get26_str.replace(' ', 'N')
tag_str = our_str[3 * rounded:].replace('a', '╬')
tag1_str = tag_str.replace('b', '↓')
tag2_str = tag1_str.replace('c', '<')
tag3_str = tag2_str.replace('d', '╦')
tag4_str = tag3_str.replace('e', '-')
tag5_str = tag4_str.replace('f', 'δ') #skipped 360
tag6_str = tag5_str.replace('g', '▼')
tag7_str = tag6_str.replace('h', '╫')
tag8_str = tag7_str.replace('i', '`')
tag9_str = tag8_str.replace('j', 'û')
tag10_str = tag9_str.replace('k', '±')
tag11_str = tag10_str.replace('l', '⌡')
tag12_str = tag11_str.replace('m', '≈')
tag13_str = tag12_str.replace('n', ']')
tag14_str = tag13_str.replace('o', '╗')
tag15_str = tag14_str.replace('p', '┐')
tag16_str = tag15_str.replace('q', '>')
tag17_str = tag16_str.replace('r', '╞')
tag18_str = tag17_str.replace('s', '╟') #627
tag19_str = tag18_str.replace('t', '╘')
tag20_str = tag19_str.replace('u', 'L')
tag21_str = tag20_str.replace('v', 'v') #353
tag22_str = tag21_str.replace('w', '#')
tag23_str = tag22_str.replace('x', 'R')
tag24_str = tag23_str.replace('y', ';')
tag25_str = tag24_str.replace('z', '~')
tag26_str = tag25_str.replace(' ', '▓')
print(new26_str + old26_str + get26_str + tag26_str)
elif 2 < rand <= 4 #abdc
new_str = our_str[:rounded].replace('a', ']')
new1_str = new_str.replace('b', '◙')
new2_str = new1_str.replace('c', '♂')
new3_str = new2_str.replace('d', 'd')
new4_str = new3_str.replace('e', 'e')
new5_str = new4_str.replace('f', '▓')
new6_str = new5_str.replace('g', '─')
new7_str = new6_str.replace('h', 'Φ')
new8_str = new7_str.replace('i', 'Θ')
new9_str = new8_str.replace('j', '≥')
new10_str = new9_str.replace('k', '≤')
new11_str = new10_str.replace('l', 'L')
new12_str = new11_str.replace('m', 'M')
new13_str = new12_str.replace('n', 'V')
new14_str = new13_str.replace('o', 'W')
new15_str = new14_str.replace('p', '►')
new16_str = new15_str.replace('q', '◄')
new17_str = new16_str.replace('r', '→')
new18_str = new17_str.replace('s', '←')
new19_str = new18_str.replace('t', 't')
new20_str = new19_str.replace('u', 'u')
new21_str = new20_str.replace('v', '~')
new22_str = new21_str.replace('w', '⌂')
new23_str = new22_str.replace('x', '°')
new24_str = new23_str.replace('y', '∙')
new25_str = new24_str.replace('z', '☻')
new26_str = new25_str.replace(' ', 'f')
old_str = our_str[rounded:2 * rounded].replace('a', 'Y')
old1_str = old_str.replace('b', 'É')
old2_str = old1_str.replace('c', 'Θ')
old3_str = old2_str.replace('d', '╩')
old4_str = old3_str.replace('e', 'b')
old5_str = old4_str.replace('f', '█')
old6_str = old5_str.replace('g', '=')
old7_str = old6_str.replace('h', '↑')
old8_str = old7_str.replace('i', 'U')
old9_str = old8_str.replace('j', '⌂')
old10_str = old9_str.replace('k', '┬')
old11_str = old10_str.replace('l', 'æ')
old12_str = old11_str.replace('m', '◄')
old13_str = old12_str.replace('n', '1')
old14_str = old13_str.replace('o', 'B')
old15_str = old14_str.replace('p', 'Ñ')
old16_str = old15_str.replace('q', '╡')
old17_str = old16_str.replace('r', '▌')
old18_str = old17_str.replace('s', '♣')
old19_str = old18_str.replace('t', 'Γ')
old20_str = old19_str.replace('u', 'τ')
old21_str = old20_str.replace('v', '╔')
old22_str = old21_str.replace('w', '░')
old23_str = old22_str.replace('x', '┘')
old24_str = old23_str.replace('y', 'ó')
old25_str = old24_str.replace('z', ';')
old26_str = old25_str.replace(' ', 'ß')
get_str = our_str[3 * rounded:].replace('a', 'ç')
get1_str = get_str.replace('b', '1')
get2_str = get1_str.replace('c', '♣')
get3_str = get2_str.replace('d', 'ⁿ')
get4_str = get3_str.replace('e', '♠')
get5_str = get4_str.replace('f', 'S')
get6_str = get5_str.replace('g', '°')
get7_str = get6_str.replace('h', '♫')
get8_str = get7_str.replace('i', 'D')
get9_str = get8_str.replace('j', '│')
get10_str = get9_str.replace('k', '←')
get11_str = get10_str.replace('l', 'ƒ')
get12_str = get11_str.replace('m', 'Æ')
get13_str = get12_str.replace('n', 'P')
get14_str = get13_str.replace('o', '#') #skipped 0781, 6406
get15_str = get14_str.replace('p', '.') #0899
get16_str = get15_str.replace('q', '┤') #0348
get17_str = get16_str.replace('r', 'µ')
get18_str = get17_str.replace('s', 'E')
get19_str = get19_str.replace('t', '§')
get20_str = get20_str.replace('u', '▬')
get21_str = get21_str.replace('v', 'û')
get22_str = get22_str.replace('w', '♀')
get23_str = get23_str.replace('x', '&')
get24_str = get24_str.replace('y', '≈')
get25_str = get25_str.replace('z', 'ª') #4460
get26_str = get26_str.replace(' ', 'N')
tag_str = our_str[2 * rounded: 3 * rounded].replace('a', '╬')
tag1_str = tag_str.replace('b', '↓')
tag2_str = tag1_str.replace('c', '<')
tag3_str = tag2_str.replace('d', '╦')
tag4_str = tag3_str.replace('e', '-')
tag5_str = tag4_str.replace('f', 'δ') #skipped 360
tag6_str = tag5_str.replace('g', '▼')
tag7_str = tag6_str.replace('h', '╫')
tag8_str = tag7_str.replace('i', '`')
tag9_str = tag8_str.replace('j', 'û')
tag10_str = tag9_str.replace('k', '±')
tag11_str = tag10_str.replace('l', '⌡')
tag12_str = tag11_str.replace('m', '≈')
tag13_str = tag12_str.replace('n', ']')
tag14_str = tag13_str.replace('o', '╗')
tag15_str = tag14_str.replace('p', '┐')
tag16_str = tag15_str.replace('q', '>')
tag17_str = tag16_str.replace('r', '╞')
tag18_str = tag17_str.replace('s', '╟') #627
tag19_str = tag18_str.replace('t', '╘')
tag20_str = tag19_str.replace('u', 'L')
tag21_str = tag20_str.replace('v', 'v') #353
tag22_str = tag21_str.replace('w', '#')
tag23_str = tag22_str.replace('x', 'R')
tag24_str = tag23_str.replace('y', ';')
tag25_str = tag24_str.replace('z', '~')
tag26_str = tag25_str.replace(' ', '▓')
print(new26_str + old26_str + tag26_str + get26_str)
hi = input("Bye")
our_str=input(“Mornin”)
长度=(长度(我们的长度)/2)
圆形=圆形(长度)
随机输入
rand=random.randint(1,6)
打印(兰特)
如果兰德
- 第一行:
our_str=input(“Mornin”)
我们的\u str应转换为整数。
- 你的条件句不对
elif 2
elif ((rand > 2) and (rand <= 4)):
# Do your stuff here
elif((rand>2)和(rand1st错误:您在以下语句中使用了变量,但没有首先初始化它们
get19_str = get19_str.replace('t', '§')
get20_str = get20_str.replace('u', '▬')
get21_str = get21_str.replace('v', 'û')
get22_str = get22_str.replace('w', '♀')
get23_str = get23_str.replace('x', '&')
get24_str = get24_str.replace('y', '≈')
get25_str = get25_str.replace('z', 'ª') #4460
get26_str = get26_str.replace(' ', 'N')
第二个错误:如果语句不允许在不使用逻辑运算符连接两个变量的情况下对单个变量执行两个关系运算(如果是n
关系运算,则应使用n-1
逻辑运算符将它们全部合并)
检查整数a是否大于2且小于5
if (a>2) and (a<5):
#statements
if(a>2)和(a>2)使用一个循环和一个列表,成为你的新好友。例如:tt=str.maketrans(“abc”,“XYZ”);assert“ZYZ”==“cbc”。translate(tt)
需要一些编辑,这是个好问题。。。