【问题标题】:While Loop Fail - Caesar CipherWhile 循环失败 - 凯撒密码
【发布时间】:2012-11-27 00:18:44
【问题描述】:

我遇到了一个问题,当我要求我的程序退出时,它会像我要求的那样打印,但也会连续显示我的选项菜单。

所以我得到了这个:

>>> 
(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: 

【问题讨论】:

  • 除了正确答案之外,您还可以考虑使用 while x.lower() != 'q': ... else: print ('Goodbye') 。在我看来,这样可以节省 if 语句并更清楚地表达意图。

标签: python loops while-loop shift encryption


【解决方案1】:

x != 'q' or 'Q' 被当作(x != 'q') or 'Q' 处理,'Q' 始终为 True。

最好是: x not in 'qQ'x.lower() != 'q'

【讨论】:

  • 你在代码中重复自己,这是一个坏习惯。我建议你在while True: 循环中执行所有操作,如果用户想退出,break。 'while' 循环的这类问题经常出现。
【解决方案2】:

你的问题是这一行:

while x != 'q' or 'Q' :

问题是Q 本身总是会返回True,所以表达式总是为真。尝试更改行:

while x != 'q' and x != 'Q' :

【讨论】:

  • 这会有所帮助,但是如果我随机播放一条消息,然后尝试退出,程序会停止而不打印“再见!”的退出消息。这是为什么呢?
  • 这是因为 print("Goodbye!") 出现在 while 循环之前。你可以在while循环的底部添加if x == 'q' or x == 'Q': print("Goodbye!")
  • 我试图把它放在while循环的底部,但是现在它又重新打印了菜单。
  • 我在底部的原始帖子中添加了我编辑的代码,以及我现在收到的输出。
  • 确保它在 while 循环内,就在 input 之后
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-25
  • 2014-03-07
  • 2020-05-31
  • 2014-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多