Hello everybody I have been trying to get around the problem of checking for a tuple in raw input and I just got a tip that kind of works but it only works for the second for loop. In other words when you input and of the 'close' words python returns 'thanx for chattin' like it should but if you input any of the 'bad' words it dosnt return anything. I have no idea what the problem is so any help would be much appreciated.
Thank you.
PS I dont know why ,my postings will never indent properly but i think i have them all right. -
print 'hello'
-
def loop():
-
bit=0
-
bad=('lol','lawl','teehee','rofl','lmao','lmfao')
-
close=('close','goodbye','shutup','be quiet')
-
response=raw_input()
-
for word in bad:
-
if word in response:
-
bit=1
-
if bit:
-
print 'we dont use that language here'
-
else:
-
print 'i dont understand your gibberish'
-
break
-
break
-
for word2 in close:
-
if word2 in response:
-
bit=1
-
if bit:
-
print 'thanx for chattin'
-
else:
-
print 'i dont understand your gibberish'
-
break
-
-
-
-
loop ()
-
loop ()
4 2424
Hello everybody I have been trying to get around the problem of checking for a tuple in raw input and I just got a tip that kind of works but it only works for the second for loop. In other words when you input and of the 'close' words python returns 'thanx for chattin' like it should but if you input any of the 'bad' words it dosnt return anything. I have no idea what the problem is so any help would be much appreciated.
Thank you.
PS I dont know why ,my postings will never indent properly but i think i have them all right. -
print 'hello'
-
def loop():
-
bit=0
-
bad=('lol','lawl','teehee','rofl','lmao','lmfao')
-
close=('close','goodbye','shutup','be quiet')
-
response=raw_input()
-
for word in bad:
-
if word in response:
-
bit=1
-
if bit:
-
print 'we dont use that language here'
-
else:
-
print 'i dont understand your gibberish'
-
break
-
break
-
for word2 in close:
-
if word2 in response:
-
bit=1
-
if bit:
-
print 'thanx for chattin'
-
else:
-
print 'i dont understand your gibberish'
-
break
-
-
-
-
loop ()
-
loop ()
The in operator can be used in a very cool way, here. It will simplify your logic greatly: -
>>> bad=('lol','lawl','teehee','rofl','lmao','lmfao')
-
>>> close=('close','goodbye','shutup','be quiet')
-
>>> def CheckInputInTuple(word):
-
... if word in bad:
-
... print 'we dont use that language here'
-
... elif word in close:
-
... print 'thanx for chattin'
-
... else:
-
... print 'i dont understand your gibberish'
-
...
-
>>> def GetUserInput():
-
... word = ""
-
... while word.lower() not in close:
-
... word = raw_input("Let's chat: ")
-
... CheckInputInTuple(word.lower())
-
...
-
>>> GetUserInput()
Would "ROFL" also be considered bad?
Ok cool now could you please explain this line by line to me because I am a bit confused as to how this works. Thank you so much for your input already.
Hi ok nevermind about that last comment I figured out what it does but now there it wont work for multiple word inputs such as "lol that was funny". When that is input it brings up "I dont understand your gibberish" becuase i can only check to see if the "bad" words are in a one word raw input. Any ways to fix this problem?
thanks
bvdet 2,851
Expert Mod 2GB
Hi ok nevermind about that last comment I figured out what it does but now there it wont work for multiple word inputs such as "lol that was funny". When that is input it brings up "I dont understand your gibberish" becuase i can only check to see if the "bad" words are in a one word raw input. Any ways to fix this problem?
thanks
I modified Barton's code for phrases instead of single words. - bad = ('lol','lawl','teehee','rofl','lmao','lmfao')
-
close = ('close','goodbye','shutup','be quiet')
-
-
def CheckInputInTuple(phrase):
-
for word in phrase.split():
-
if word in bad:
-
return 1 #'we dont use that language here'
-
elif word in close:
-
return 0 #'thanx for chattin'
-
return 2 # 'i dont understand your gibberish'
-
-
def GetUserInput():
-
while True:
-
phrase = raw_input("Let's chat: ")
-
ans = CheckInputInTuple(phrase.lower())
-
if ans == 1:
-
print 'we dont use that language here'
-
elif not ans:
-
print 'thanx for chattin'
-
break
-
else:
-
print 'i dont understand your gibberish'
-
-
GetUserInput()
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Thorsten Kampe |
last post by:
I found out that I am rarely using tuples and almost always lists
because of the more flexible usability of lists (methods, etc.)
To my knowledge, the only fundamental difference between tuples...
|
by: Carlos Ribeiro |
last post by:
As a side track of my latest investigations, I began to rely heavily
on generators for some stuff where I would previsouly use a more
conventional approach. Whenever I need to process a list, I'm...
|
by: adam |
last post by:
i'm working on a portion of a CMS that allows content-admins to browse
a product list, and add individual products into the taxonomy by
clicking checkboxes next to categories they might belong in....
|
by: Grasshopper |
last post by:
Hi,
I am automating Access reports to PDF using PDF Writer 6.0. I've
created a DTS package to run the reports and schedule a job to run this
DTS package. If I PC Anywhere into the server on...
|
by: Martin v. Löwis |
last post by:
I've been working on PEP 353 for some time now.
Please comment, in particular if you are using 64-bit
systems.
Regards,
Martin
PEP: 353
Title: Using ssize_t as the index type
Version:...
|
by: ckoniecny |
last post by:
I have the following two files:
File1:
11 John Doe
33 Jane Doe
55 Steve Smith
File2:
22 Joe Doe
44 Willy Widget
|
by: Selpher |
last post by:
Hi guys, I'm having trouble getting these two for loops to interact properly. The point of this program is that a number inputed by the user (we'll use 15 as an example) is supposed to display...
|
by: OBAFGKM_RNS |
last post by:
Let's say I have a Javascript function that loops over and over. In
that function i have it alternating images on a button this way:
if(var==0){
var myHTML = "<input type='button'...
|
by: ian.team.python |
last post by:
To step through a list, the python style is avoid an explicit index.
But what if the same hidden index is to be used for more than one list
for example:-
for key,value in listKeys,listValues :...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |