2012-11-27 36 views
0

我有一个问题,当我要求我的程序退出时,打印就像我问它,但显示我的选项菜单以及不断。虽然循环失败 - 凯撒密码

所以我得到这样的:

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

我希望程序洗牌的消息,或unshuffling消息后,以及在开始显示菜单,而不是在用户已经要求通过选择“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: 
+0

除了你可以考虑使用正确的答案了'而x.lower()=“Q”!......还有:打印(“再见” )'。这节省了一个if语句,并在我看来更清楚地表达了意图。 – Pyranja

回答

0

你的问题是这样的一行:

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

的问题是,Q本身总是返回True,所以表达将永远是真实的。尝试改变符合:

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

这有助于解决问题,但是如果我随机播放一条消息,然后尝试退出,则程序会停止而不打印“再见!”退出消息。这是为什么? –

+0

这是因为print(“Goodbye!”)出现在while循环之前。您可以在while循环的底部添加'if x =='q'或x =='Q':print(“Goodbye!”)'。 –

+0

我试着把它放在while循环的底部,但是现在它再次重新打印菜单。 –

4

x != 'q' or 'Q'被处理为(x != 'q') or 'Q',和“Q”始终是真实的。

这将更好的是: x not in 'qQ'x.lower() != 'q'

+0

你正在重复自己的代码,这是一个坏习惯。我建议你在'while True:'循环中做所有事情,并且如果用户想要退出,'break'。这种'while'循环的问题经常出现。 –