循环失败 - 凯撒密码 [英] While Loop Fail - Caesar Cipher

查看:165
本文介绍了循环失败 - 凯撒密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,当我要求我的程序退出它打印像我要求它,但显示我的选项菜单不断。

I'm having a problem where when I ask my program to quit out it prints like I ask it to, however shows my menu of options as well continuously.

所以我得到这个:

>>> 
(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循环错误,或者是我的代码中的某些东西?

Here is my complete code, please explain why my menu is reprinting, did I make my while loop wrong, or is something out of place in my code?

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退出程序之后。

I want the program to display the menu after shuffling a message, or unshuffling a message, as well as at the beginning, but not after the user has asked to quit the program by choosing '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 != 'q' or 'Q' :

问题是 Q 本身将始终返回 True ,所以表达式将永远是真的。尝试更改行:

The problem is that Q itself will always return True, so the expression will always be true. Try changing the line with:

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

这篇关于循环失败 - 凯撒密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆