I am trying to build a Twitter filter app that searches your personal timeline of people you follow/RTs from people you follow from keywords of your choosing. (That's not the question, that's the background... I'm not up to that yet, I'm just playing around with the API at the moment!)
I'm just starting to learn Python, I'm previously a Java programmer and I'm wondering how to check user input for valid... well, input!
I have a numbered menu (currently with just 2 items in) and I want the user to type 1 or 2 and if they don't, for it to throw an error message and loop back to the input. I'm currently getting the error message of:
Traceback (most recent call last):
File "Filtwer.py", line 31, in <module>
if "1" in menuselect:
TypeError: argument of type 'int' is not iterable
Where line 31 is the start of the if statement in the code block below. I'm not sure if I'm missing something? Not breaking out of the while loop correctly for example? Any help would be much appreciated!
Thanks :)
import twitter
api = twitter.Api(consumer_key='<redacted>',
consumer_secret='<redacted>',
access_token_key='<redacted>',
access_token_secret='<redacted>')
while True:
menuselect = input("1. Tweet\n2. Get Tweets\n...: ")
if menuselect == 1 or 2: break
print "Please enter a valid entry!"
if "1" in menuselect:
statusinput = raw_input("Tweet: ")
status = api.PostUpdate(statusinput)
print "Sucessfully tweeted!"
else:
timeline5 = api.GetUserTimeline(user_id=<my_twitter_ID>, screen_name='<my_twitter_screenname>', count=5, include_rts='true')
print [s.text for s in timeline5]
Edit:
Got it to work like this (included comments to show how my answer differed from the answer I chose as correct. Thanks for the help guys! :) )
while True:
#try:
menuselect = raw_input("1. Tweet\n2. Get Tweets\n...: ")
if menuselect == "1" or menuselect == "2": break
#catch ValueError:
# pass
#finally:
print "Please enter a valid entry!"
if "1" == menuselect:
[...]
To check if menuselect
is 1
, you should use ==
operator NOT in
operator. (Because in
operator is used to check the member existence)
if 1 == menuselect:
Note: Also, don't use input
function in Python 2.x, to get input from the user (because of the security concerns). Use raw_input
instead and convert the result to int
manually, like this
menuselect = int(raw_input("1. Tweet\n2. Get Tweets\n...: "))
Note 2: You might want to use in
operator in the break
condition (it is correct here, because you are checking whether the value of menuselect
is one of the possible values), like this
if menuselect in (1, 2): break
because
if menuselect == 1 or 2:
will always evaluate to True
, as it is evaluated as (menuselect == 1) or 2
, even if menuselect
is not 1, the 2
part will make the expression evaluate to Truthy.
Edit: To get around the exception part, when a string is entered instead of integers, you can use try..except
like this
while True:
try:
menuselect = input("1. Tweet\n2. Get Tweets\n...: ")
if menuselect int (1, 2): break
catch ValueError:
pass
finally:
print "Please enter a valid entry!"
Another problem you have is with this line:
if menuselect == 1 or 2: break
Python interprets this as
if (menuselect == 1) or 2: break
Meaning it checks if menuselect
is one, and if it is false, it checks if 2
is true. In python, non-zero numbers are interpreted as true, so this condition will always be true.
Change this to either
if menuselect in [1, 2]: break
or the more lengthy
if menuselect == 1 or menuselect == 2: break
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.