944,161 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 561
  • Python RSS
Nov 4th, 2009
0

Number 1 most possible frustrating situation

Expand Post »
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:

Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Nov 5th, 2009
0
Re: Number 1 most possible frustrating situation
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.

python Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 19
Junior Poster
ShadyTyrant is offline Offline
120 posts
since Nov 2009
Nov 5th, 2009
0
Re: Number 1 most possible frustrating situation
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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Nov 5th, 2009
0
Re: Number 1 most possible frustrating situation
Wow I didn't catch that. I ran it using 2.5 and it seemed to work so I assumed.
Reputation Points: 10
Solved Threads: 19
Junior Poster
ShadyTyrant is offline Offline
120 posts
since Nov 2009
Nov 5th, 2009
0
Re: Number 1 most possible frustrating situation
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.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 5th, 2009
0
Re: Number 1 most possible frustrating situation
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.

Quote ...
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.
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Nov 5th, 2009
0
Re: Number 1 most possible frustrating situation
Try this trick (I'm assuming you're using Windows)

1) Open up a terminal (command prompt window)
Python Syntax (Toggle Plain Text)
  1. Ctrl+R -> cmd -> [Enter]
2) Type/find path to Python executable
Python Syntax (Toggle Plain Text)
  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:
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
Nov 5th, 2009
0
Re: Number 1 most possible frustrating situation
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.
Reputation Points: 10
Solved Threads: 19
Junior Poster
ShadyTyrant is offline Offline
120 posts
since Nov 2009
Nov 5th, 2009
0
Re: Number 1 most possible frustrating situation
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.
Reputation Points: 14
Solved Threads: 17
Junior Poster
AutoPython is offline Offline
138 posts
since Sep 2009
Nov 5th, 2009
0
Re: Number 1 most possible frustrating situation
If you declared the encoding at the beginning of your program the same way that IDLE does, it would have worked I believe?
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Alphabetizer
Next Thread in Python Forum Timeline: OS X default save path





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC