Number 1 most possible frustrating situation

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster

Number 1 most possible frustrating situation

 
0
  #1
Nov 4th, 2009
Okay, the encryption algorithm idea was stupid. But I still made an encryption program. I got the absolute most possible frustrating situation. The program doesn't work in the console window. It spits out a giant error (probably recursion judging how many errors it got), but it DOES work in IDLE. So the only way to fix it is manually see the error, which I've tried to do again and again with no success. So, the error comes after you input the message. So here's the code:

  1. import os
  2. import random
  3.  
  4. if os.name == "nt":
  5. os.system("color 48")
  6. os.system("title Encryptor/Decryptor")
  7. def clear():
  8. os.system("cls")
  9. else:
  10. def clear():
  11. os.system("clear")
  12.  
  13. def menu():
  14. clear()
  15. print ("")
  16. print ("Encryptor/Decryptor")
  17. print ("-------------------")
  18. print ("1) Encrypt Message ")
  19. print ("2) Decrypt Message ")
  20. print ("-------------------")
  21. menu_choice = input ("Choose one: ")
  22.  
  23. if menu_choice != "1":
  24. if menu_choice != "2":
  25. clear()
  26. print ("")
  27. print ("Please type a valid choice!")
  28. input ()
  29. menu()
  30.  
  31. if menu_choice == "1": encrypt()
  32. if menu_choice == "2": decrypt()
  33.  
  34. def encrypt():
  35. clear()
  36. print ("")
  37. print ("------------------------------------")
  38. print ("1) Use computer generated number key")
  39. print ("2) Manually key ")
  40. print ("------------------------------------")
  41. encrypt_choice = input ("Choose one: ")
  42.  
  43. if encrypt_choice == "1":
  44. key = list(str(random.random()).split(".")[1])
  45. else:
  46. clear()
  47. print ("")
  48. key = list(str(input("Please type key: ")))
  49.  
  50. show = ""
  51. while 1:
  52. clear()
  53. print ("")
  54. print (show)
  55. print ("Type S to show key, H to hide key ")
  56. print ("Type R to change key ")
  57. print ("-----------------------------------")
  58. message = list(input ("Please type a message to be encrypted: "))
  59.  
  60. if "".join(message).lower() == "s":
  61. show = str("Key is: " + str("".join(key)))
  62. continue
  63. if "".join(message).lower() == "r":
  64. encrypt()
  65. if "".join(message).lower() == "h":
  66. show = ""
  67. continue
  68.  
  69. break
  70.  
  71.  
  72. while len(key) < len(message):
  73. key += key
  74.  
  75. spaces = 0
  76. encrypt_list = []
  77. shift = 1
  78. for loop in range((len(message))):
  79. shift += 1
  80. if str(message[loop]) == ' ':
  81. spaces += 1
  82. else:
  83. if str(message[loop]) == ' ':
  84. spaces += 1
  85. ''
  86. if str(message[loop]) != ' ':
  87. if str(message[loop]) != ' ':
  88. if str(message[loop]) == str(key[loop-spaces]):
  89. encrypt_list.append(message[loop])
  90. else:
  91. if (shift % 2) == 0:
  92. loop_ord = str(chr(ord(key[loop-spaces])+ord(message[loop])))
  93. else:
  94. if ord(str(message[loop])) < ord(str(key[loop-spaces])):
  95. loop_ord = "9u" + str(chr(ord(key[loop-spaces])-ord(message[loop])))
  96. else:
  97. loop_ord = chr(ord(message[loop])-ord(key[loop-spaces]))
  98. encrypt_list.append(loop_ord)
  99.  
  100. print ("")
  101. print ("Encrypted message is: ", "".join(encrypt_list))
  102. if show == "":
  103. print ("")
  104. ask = input ("Show key before leaving(Y/N): ")
  105. if ask.lower() == "y":
  106. print ("")
  107. print ("Key is: " + str("".join(key)))
  108. input ()
  109. menu()
  110.  
  111. def decrypt():
  112. menu()
  113.  
  114. menu()

Help would GREATLY be appreciated.
Last edited by AutoPython; Nov 4th, 2009 at 10:58 pm.
To P(ython), or not to P(ython)
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 75
Reputation: ShadyTyrant is an unknown quantity at this point 
Solved Threads: 9
ShadyTyrant's Avatar
ShadyTyrant ShadyTyrant is offline Offline
Junior Poster in Training
 
0
  #2
Nov 5th, 2009
Well already I see one problem. You are using the input() function instead of using raw_input(). Input only receives int data types. Though when you try to compare the user input to a valid choice you are comparing it to a string. Use raw_input() because it reads in strings instead and this should solve that problem.

Edit: Also you need not use so many print statements. You can print lines like so.

  1. # using triple quoted strings
  2.  
  3. print '''What would you like to do?
  4. 1) This
  5. 2) Or this
  6. 3) Exit
  7. '''
Last edited by ShadyTyrant; Nov 5th, 2009 at 12:57 pm. Reason: Added more info
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #3
Nov 5th, 2009
Originally Posted by ShadyTyrant View Post
You are using the input() function instead of using raw_input().
He also is using parenthesis in his print statements. I'd surmise that he's using Python 3.0, in which case the use of input is the only option. ( input has been replaced by raw_input )

EDIT: Additionally, I tried his code (after modifying it to be Python 2.X compliant) and it worked for the most part. To the OP: could you explain the error in more detail and provide some traceback?
Last edited by jlm699; Nov 5th, 2009 at 1:26 pm.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 75
Reputation: ShadyTyrant is an unknown quantity at this point 
Solved Threads: 9
ShadyTyrant's Avatar
ShadyTyrant ShadyTyrant is offline Offline
Junior Poster in Training
 
0
  #4
Nov 5th, 2009
Wow I didn't catch that. I ran it using 2.5 and it seemed to work so I assumed.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,140
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 946
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite
 
0
  #5
Nov 5th, 2009
I want to encourage posters to give us the version of Python they are using. Otherwise the rest of us have to assume and ask. Python25 for instance allows you to use the print statement or the print() function. So using the fact that print() was used is not necessarily indicative of the a Python3 application.

Not quite sure what the OP means with it DOES work in IDLE, unless IDLE uses Python3 and his system default on saved files is still Python2.
Last edited by vegaseat; Nov 6th, 2009 at 5:07 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster
 
0
  #6
Nov 5th, 2009
No, system default for files and IDLE are the same, I can't explain the error to you because the console window closes before I can see it, but IDLE gives no error.

Well already I see one problem. You are using the input() function instead of using raw_input().
Do you think this isn't my code? How could I make code like this if I didn't even know what function to use.
I hope you understand my problem now, I don't know why every thing is working in idle, but not the console.
Last edited by AutoPython; Nov 5th, 2009 at 5:12 pm.
To P(ython), or not to P(ython)
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #7
Nov 5th, 2009
Try this trick (I'm assuming you're using Windows)

1) Open up a terminal (command prompt window)
  1. Ctrl+R -> cmd -> [Enter]
2) Type/find path to Python executable
  1. dir C:\Py* -> [Enter]
  2. # You should see the folder name for your Python install (I'm guessing it's Python30
  3. C:\Python30\python.exe -> Drag-and-drop your python script to the command prompt window (where you're typing this stuff), which should auto-insert the full path to your python script -> [Enter]
  4.  
This should get you running your python script inside a persistent window that won't close itself. And example command would be:
  1. C:\> C:\Python25\python.exe "C:\Documents and Settings\Administrator\Desktop\em_test.py"
Last edited by jlm699; Nov 5th, 2009 at 5:28 pm.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 75
Reputation: ShadyTyrant is an unknown quantity at this point 
Solved Threads: 9
ShadyTyrant's Avatar
ShadyTyrant ShadyTyrant is offline Offline
Junior Poster in Training
 
0
  #8
Nov 5th, 2009
I wasn't trying to say you didn't write the code. It was just an error that popped up for me when I tried to run the code. Though jlm699 corrected me already. Anyways since the console is closing before you can see the error, open a console first and run the program from the command line. That way when the program throws the error the console wont close.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 112
Reputation: AutoPython is an unknown quantity at this point 
Solved Threads: 9
AutoPython's Avatar
AutoPython AutoPython is offline Offline
Junior Poster
 
0
  #9
Nov 5th, 2009
Thank you for the help. But the problem is solved, it wasn't the encrypting part (thank goodness), but when it attempted to print some of the unicode characters with 2 symbols it couldn't. Still don't know how IDLE found a way around it.
Last edited by AutoPython; Nov 5th, 2009 at 9:46 pm.
To P(ython), or not to P(ython)
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #10
Nov 5th, 2009
If you declared the encoding at the beginning of your program the same way that IDLE does, it would have worked I believe?
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 296 | Replies: 9
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC