473,811 Members | 2,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using For Loops to check for tuples in raw input

21 New Member
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 2435
bartonc
6,596 Recognized Expert Expert
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
PythonNotSoGuru
21 New Member
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
PythonNotSoGuru
21 New Member
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 Recognized Expert Moderator Specialist
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
2123
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 and lists is that tuples are immutable, so if this is correct, than list are a superset of tuples, meaning lists can do everything tuples can do and more. Is there any advantage for using tuples? Are they "faster"? Consume less memory? When is...
3
1955
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 tending towards the use of generators. One good example is if I want to print a report, or to work over a list with complex processing for each item. In both cases, a simple list comprehension can't be used. The conventional approach involves...
7
3361
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. since the taxonomy is a rather long list, i'm hiding and showing divs for the secondary and tertiary links, so when a user clicks on the checkbox for the parent category, the children appear in a second (and third) div, with checkboxes of their...
11
6608
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 where the job is running, the job runs sucessfully, PDF files got generated, everything is good. If I scheduled the job to run at the time that I am not logged into the server, Access is not able to print to the printer. The error is pretty...
2
1887
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: $Revision: 42333 $
5
5870
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
6089
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 solutions where when numbers are added together in sequential order, they will sum up to be the origianl inputed number. For example, inputting 15 is supposed to yield the following: 15 = 1 + 2 + 3 + 4 + 5 15 = 4 + 5 + 6 15 = 7 + 8 Since it must be...
2
3582
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' style='background-image: url(grnbutt.jpg);etc. etc.'>"; var outq = document.getElementById("qbutt"); outq.innerHTML = myHTML; } else{
8
1321
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 : newdict=value won't work as it is a tuple of lists, as opposed to a list of tuples. Is there an elegant solution to this? Is there a way to merge lists into a list of tuples to allow moving through multiple lists, or is
0
9722
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10379
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10393
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10124
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.