473,386 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Using For Loops to check for tuples in raw input

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.
Expand|Select|Wrap|Line Numbers
  1. print 'hello'
  2. def loop():
  3.     bit=0
  4.     bad=('lol','lawl','teehee','rofl','lmao','lmfao')
  5.     close=('close','goodbye','shutup','be quiet')
  6.     response=raw_input()
  7.     for word in bad:
  8.         if word in response:
  9.             bit=1
  10.             if bit:
  11.                 print 'we dont use that language here'
  12.             else:
  13.                 print 'i dont understand your gibberish'
  14.             break
  15.         break
  16.     for word2 in close:
  17.         if word2 in response:
  18.             bit=1
  19.             if bit:
  20.                 print 'thanx for chattin'
  21.             else:
  22.                 print 'i dont understand your gibberish'
  23.             break      
  24.  
  25.  
  26.  
  27.     loop ()
  28. loop ()
Oct 27 '07 #1
4 2424
bartonc
6,596 Expert 4TB
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.
Expand|Select|Wrap|Line Numbers
  1. print 'hello'
  2. def loop():
  3.     bit=0
  4.     bad=('lol','lawl','teehee','rofl','lmao','lmfao')
  5.     close=('close','goodbye','shutup','be quiet')
  6.     response=raw_input()
  7.     for word in bad:
  8.         if word in response:
  9.             bit=1
  10.             if bit:
  11.                 print 'we dont use that language here'
  12.             else:
  13.                 print 'i dont understand your gibberish'
  14.             break
  15.         break
  16.     for word2 in close:
  17.         if word2 in response:
  18.             bit=1
  19.             if bit:
  20.                 print 'thanx for chattin'
  21.             else:
  22.                 print 'i dont understand your gibberish'
  23.             break      
  24.  
  25.  
  26.  
  27.     loop ()
  28. loop ()
The in operator can be used in a very cool way, here. It will simplify your logic greatly:
Expand|Select|Wrap|Line Numbers
  1. >>> bad=('lol','lawl','teehee','rofl','lmao','lmfao')
  2. >>> close=('close','goodbye','shutup','be quiet')
  3. >>> def CheckInputInTuple(word):
  4. ...     if word in bad:
  5. ...         print 'we dont use that language here'
  6. ...     elif word in close:
  7. ...         print 'thanx for chattin'
  8. ...     else:
  9. ...         print 'i dont understand your gibberish'
  10. ...         
  11. >>> def GetUserInput():
  12. ...     word = ""
  13. ...     while word.lower() not in close:
  14. ...         word = raw_input("Let's chat: ")
  15. ...         CheckInputInTuple(word.lower())
  16. ...     
  17. >>> GetUserInput()
Would "ROFL" also be considered bad?
Oct 27 '07 #2
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.
Oct 27 '07 #3
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
Oct 29 '07 #4
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.
Expand|Select|Wrap|Line Numbers
  1. bad = ('lol','lawl','teehee','rofl','lmao','lmfao')
  2. close = ('close','goodbye','shutup','be quiet')
  3.  
  4. def CheckInputInTuple(phrase):
  5.     for word in phrase.split():
  6.         if word in bad:
  7.             return 1 #'we dont use that language here'
  8.         elif word in close:
  9.             return 0 #'thanx for chattin'
  10.     return 2 # 'i dont understand your gibberish'
  11.  
  12. def GetUserInput():
  13.     while True:
  14.         phrase = raw_input("Let's chat: ")
  15.         ans = CheckInputInTuple(phrase.lower())
  16.         if ans == 1:
  17.             print 'we dont use that language here'
  18.         elif not ans:
  19.             print 'thanx for chattin'
  20.             break
  21.         else:
  22.             print 'i dont understand your gibberish'
  23.  
  24. GetUserInput()
Oct 29 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
3
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...
7
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....
11
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...
2
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:...
5
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
1
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...
2
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'...
8
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 :...
0
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,...
0
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...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.