Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
When I declare the variable for the string in the code it works, for example:
messagetoencode="readthis"
encodedMsg=[]
for letter in messagetoencode:
encodedMsg.append(encode[letter.upper()])
print "result is ", encodedMsg
However when I try and do the same operation with the user input it doesn't work:
lst = list(raw_input("Please enter a message to encode: "))
encodedMsg=[]
for letter in lst:
encodedMsg.append(encode[letter.upper()])
print "result is ", encodedMsg
and I get a traceback error, any ideas why?
Error:
Please enter a message to encode: hello
Traceback (most recent call last):
File "Untitled 3.py", line 27, in <module>
obfuse.append(encode[letter.upper()])
KeyError: 'O'
The problem is with the key O in the dictionary. It doesn't exist on the text, which is why no error happened. But if you include o in your input, you'll get this error.
Well, the real solution is to have the key'O' in your encoding dictionary.
To deal with unexpected input, you can do a check like this:
for letter in lst:
if letter not in encode:
raise KeyError("Sorry i don't know how to encode this letter!")
encodedMsg.append(encode[letter.upper()])
The problem is, O is not in the encode dictionary. If you want to assign a default value, instead of failing like this, you can use dict.get method like this
obfuse.append(encode.get(letter.upper(), None))
Now, this will return None if any of the keys are not found in encode. You might actually want to include a mapping for O in your code.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
I'm trying to make it so that I can input someone into console and have it set a variable to it, and every time with the if statement it gives the error
File "main.py", line 58, in <module>
if meInput.startswith("%send"):
AttributeError: 'builtin_function_or_method' object has no attribute 'startswith'
Here's the code:
if input.startswith("%send"):
myinput = input.split(" ", 2)[2]
channel = client.get_channel(12324234183172)
I've tried putting it into a variable such as variable = input then changing the if statement to match the variable, but it does the same thing.
Read the error message carefully! It is telling you that input is not a string, but a function — a function that would return a string if you called it, but you didn’t. Try this instead:
if input().startswith("%send"):
Note the parentheses. That is how you call a function in Python, and in most other languages.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm having trouble getting Python's .upper and .lower methods to return anything. Here's the code:
initials = input("Enter your initials: ")
uppercase = initials.upper
print(uppercase)
What it returns is:
Enter your initials: mj
<built-in method upper of str object at 0x7f50734b01f0>
I originally integrated this into a larger function, but when I call uppercase later in the function the variable remains empty. I'm working in Google Colab.
To call a method in Python, you must open and close parenthesis after the name of the method:
uppercase = initials.upper()
See the example in the documentations of str.upper.
Here you go, just make sure you use .upper() method appropriately with your variable. :)
initials = input("Enter your initials: ")
print(initials.upper())
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Solved: Thanks for your helps. i'm working dictionary and hash codes in one page, and there was a block problem. i thought, my logic is wrong but i checked all page, and i fixed blocks. its works fine right now.
Here's my problem:
d = {}
d['a'] = 'alpha'
d['b'] = 'beta'
missing_key = 'x' in d
if missing_key == True:
print ("The key, you are looking for was successfully found!")
else:
print ("The key, you are looking for was not found!")
print ("Here are keys in database:")
for k, r in enumerate(d.keys(), start=1)
print ("Key {}: {}".format(k, r))
that for condition works perfectly. but i cant run that if condition. Where am i doing wrong ? Thanks for your help.
Getting this error:
File "C:/Python/dictionary-hash.py", line 33
if missing_key == True:
IndentationError: unexpected indent
also i'm using "Python 3.6" and "Anaconda Spyder"
There's nothing wrong with the logic in your code. Since the error is an IndentationError, it must be because you're using inconsistent indentation on that if line. Make sure that the indentation on each line is consistent (using same number of spaces/tabs). Probably there is a space at the beginning of that line that is causing the error.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Trying to make a code about usernames:
User=input("type a username with 4 numbers, then 2 letters.")
test=(User.isdigit[0:3])
trial=(User.isaplha[4:5])
if test ==True:
if trial ==True:
print("This is a valid username.")
else:
print("The last two characters must be numbers.")
else:
print("The first four characters must be letters.")
I receive this error
Traceback (most recent call last):
File "python", line 2, in
TypeError: 'builtin_function_or_method' object is not subscriptable
isdigit() and isalpha() don’t support indexing parameters and if you want to apply these builtin functions on specific substring try indexing your string like
test = User[0:3].isdigit()
It will work perfectly without any errors 👍🏻
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
def open_file(filename):
file_open= open(filename,"r")
return file_open
When I try and call the function I get the following results:
>>> open_file(random.txt)
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
open_file(random.txt)
NameError: name 'random' is not defined
try
open_file('random.txt')
Strings in Python need to be quoted.
random is being interpreted as an object, and is undefined.
You forgot quotes:
open_file('random.txt')
python thinks random is an object, which obviously you didn't define. The quotes make it a string.
you just need to input the filename as a string; here's how it must be done:
>>> open_file('random.txt')
note that your function works just fine, all you need to do is call it properly.