我遇到了一个问题,当我要求我的程序退出时,它会像我要求的那样打印,但是也会连续显示我的选项菜单。
所以我得到了这个:
>>>
(S)huffle a message.
(U)nshuffle a message.
(Q)uit program.
Choose a option to begin: q
Goodbye!
(S)huffle a message.
(U)nshuffle a message.
(Q)uit program.
Choose a option to continue:
如果我选择“q”,我希望它在哪里显示:
>>>
(S)huffle a message.
(U)nshuffle a message.
(Q)uit program.
Choose a option to begin: q
Goodbye!
这是我的完整代码,请解释为什么我的菜单要重新打印,我的 while 循环是否出错,或者我的代码中有什么不合适的地方?
def hw8():
print('(S)huffle a message.')
print('(U)nshuffle a message.')
print('(Q)uit program.')
x = input('Choose a option to begin: ')
if x == 'Q' or x == 'q':
print('Goodbye!')
while x != 'q' or 'Q' :
if x == 'S' or x == 's':
y = input('Enter a message to shuffle: ')
q1 = ''
for i in y:
if ord(i) in range(65,90) or ord(i) in range(97,122):
q = chr(ord(i) + 1)
q1 = q1 + q
elif ord(i) == 90:
q = chr(ord(i) + 7)
q1 = q1 + q
elif ord(i) == 122:
q = 'A'
q1 = q1 + q
else:
q = i
q1 = q1 + q
print(q1)
if x == 'U' or x == 'u':
f = input('Enter a message to unshuffle: ')
t2 = ''
for i in f:
if ord(i) in range (66,91) or ord(i) in range(98,123):
t = chr(ord(i) - 1)
t2 = t2 + t
elif ord(i) == 65:
t = 'z'
t2 = t2 + t
elif ord(i) == 97:
t = 'Z'
t2 = t2 + t
else:
t = i
t2 = t2 + t
print(t2)
print('(S)huffle a message.')
print('(U)nshuffle a message.')
print('(Q)uit program.')
x = input('Choose a option to continue: ')
hw8()
我希望程序在改组消息或解组消息后以及开始时显示菜单,但不是在用户通过选择“q”要求退出程序后显示菜单。
编辑代码:
def hw8():
print('(S)huffle a message.')
print('(U)nshuffle a message.')
print('(Q)uit program.')
x = input('Choose a option to begin: ')
while x != 'q' or x != 'Q' :
if x == 'S' or x == 's':
y = input('Enter a message to shuffle: ')
q1 = ''
for i in y:
if ord(i) in range(65,90) or ord(i) in range(97,122):
q = chr(ord(i) + 1)
q1 = q1 + q
elif ord(i) == 90:
q = chr(ord(i) + 7)
q1 = q1 + q
elif ord(i) == 122:
q = 'A'
q1 = q1 + q
else:
q = i
q1 = q1 + q
print(q1)
if x == 'U' or x == 'u':
f = input('Enter a message to unshuffle: ')
t2 = ''
for i in f:
if ord(i) in range (66,91) or ord(i) in range(98,123):
t = chr(ord(i) - 1)
t2 = t2 + t
elif ord(i) == 65:
t = 'z'
t2 = t2 + t
elif ord(i) == 97:
t = 'Z'
t2 = t2 + t
else:
t = i
t2 = t2 + t
print(t2)
print('(S)huffle a message.')
print('(U)nshuffle a message.')
print('(Q)uit program.')
x = input('Choose a option to continue: ')
if x == 'Q' or x == 'q':
print('Goodbye!')
hw8()
新输出:
>>> ================================ RESTART ================================
>>>
(S)huffle a message.
(U)nshuffle a message.
(Q)uit program.
Choose a option to begin: s
Enter a message to shuffle: hello
ifmmp
(S)huffle a message.
(U)nshuffle a message.
(Q)uit program.
Choose a option to continue: q
Goodbye!
(S)huffle a message.
(U)nshuffle a message.
(Q)uit program.
Choose a option to continue: