Posts: 2
Threads: 1
Joined: Jun 2017
Hello all,
I would like some expert python help. I'm very new to Python and I'm trying to create a simple caesar but it isn't working. It's stuck on the selection screen and it won't go any further.
def decrypt():
a=raw_input("Give me the word to decrypt:")
number=input("What was it shifted by?")
b=list(a)
str(b)
c=[ord(x)for x in(b)]
d=[]
for i in c:
d.append(i-number)
e=[chr(i) for i in (d)]
e="".join(e)
print ("Decryption Successful, your word is ",e,"!")
def encrypt():
a=raw_input("Give me a word:")
number=input("Give me a number:")
b=list(a)
str(b)
c=[ord(x)for x in(b)]
d=[]
for i in c:
d.append(i+number)
e=[chr(i) for i in (d)]
e="".join(e)
print ("Your Caesar shifted result (ascii code) is",e,"!")
print ("Your key is ",number," remember that!")
def menu():
print ("\n\n\nWelcome to the Caesar Shifter.")
print ("What would you like to do?")
print ("Option 1:Encrypt Word")
print ("Option 2:Decrypt Word")
print ("If you would like to quit, press 0.")
choice=input("Pick your selection: ")
if choice==1:
run=encrypt()
run
menu()
elif choice==2:
derun=decrypt()
derun
menu()
elif choice==0:
quit
else:
print ("That is not a correct selection, please pick either 1, or 2.")
menu()
Posts: 1,298
Threads: 38
Joined: Sep 2016
A quick glance shows a mix of 'input()' and 'raw_input'. If you are using Python 2.x use 'raw_input()', if you are using Python 3.x, use 'input()'.
In both cases, the variables will be strings, so you must use them as strings, not numbers. So for example in your 'menu' function, you have the comparison of a 'string' with a number:
if choice==1: # choice is a string, 1 is an integer you could resolve this by making your numbers into strings:
if choice=="1": # Note the quotes around the number 1 Another solution would be to get an integer value rather than a string:
choice = int(input("Enter a number: "))
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 2
Threads: 1
Joined: Jun 2017
Thank you for all of your help dude.
I have made those changes and it will won't work.
def decrypt():
a=input()("Give me the word to decrypt:")
number=input("What was it shifted by?")
b=list(a)
str(b)
c=[ord(x)for x in(b)]
d=[]
for i in c:
d.append(i-number)
e=[chr(i) for i in (d)]
e="".join(e)
print ("Decryption Successful, your word is ",e,"!")
def encrypt():
a=input()("Give me a word:")
number=input("Give me a number:")
b=list(a)
str(b)
c=[ord(x)for x in(b)]
d=[]
for i in c:
d.append(i+number)
e=[chr(i) for i in (d)]
e="".join(e)
print ("Your Caesar shifted result (ascii code) is",e,"!")
print ("Your key is ",number," remember that!")
def menu():
print ("\n\n\nWelcome to my program")
print ("What would you like to do?")
print ("Option 1:Encrypt Word")
print ("Option 2:Decrypt Word")
print ("If you would like to quit, press 0.")
choice=input("Pick your selection: ")
if choice=="1":
run=encrypt()
run
menu()
elif choice=="2":
derun=decrypt()
derun
menu()
elif choice=="0":
quit
else:
print ("That is not a correct selection, please pick either 1, or 2.")
menu() Are you able to repair my code. I would really like to test it.
Posts: 4,220
Threads: 97
Joined: Sep 2016
Jun-15-2017, 09:43 PM
(This post was last modified: Jun-15-2017, 09:44 PM by ichabod801.)
Your input on line 15 is wrong, you need the prompt inside the parens for the call to input. Then in line 16, remember that input is giving back a string. You need to convert that to an int before you do the adding on line 22. When you are adding, you need to wrap around the alphabet. For instance "Funky" with number = 5 becomes "Kzsp~". It should be "Kzspd". I'm guessing you will need to make similar fixes to your decrypt function. Also, menu should go in a loop, rather than recursively calling itself. And lines 37 and 41 do nothing, so they can be taken out. And please don't use single letter variable names, it makes your code harder to figure out.
Posts: 1,298
Threads: 38
Joined: Sep 2016
(Jun-15-2017, 08:16 PM)as1221 Wrote: Are you able to repair my code.
Yes, but that doesn't help you. If you run your code, you will get errors. You need to post the entire error code between the 'error' code tags. Before posting them, however, look at them and see if can decipher what it is telling you (hint: look towards the end).
To get you started, this:
a=input()("Give me the word to decrypt:") should be:
a=input("Give me the word to decrypt:") Remember what I said about strings and numbers. You are asking the user for a 'number', the way you are doing so makes 'number' a string, yet further in your code you are using it as if it is in fact a number
d.append(i - number) This will fail.
A (very) helpful piece of advise, forget the use of single character variables. Python encourages the use of descriptive variable names: 'a' is not descriptive, 'number' is. It makes your code easier to follow not only for yourself, but others that may read it.
There are other errors, but try and figure them out on your own by looking at the error codes. If you can't, post the errors and we will be glad to help you along.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 1,298
Threads: 38
Joined: Sep 2016
Beat me to it, ichabod
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Posts: 4,220
Threads: 97
Joined: Sep 2016
(Jun-15-2017, 09:54 PM)sparkz_alot Wrote: Beat me to it, ichabod Big Grin
Yeah, but your answer was clearer because you took more time on it.
|