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 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. -
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
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. - 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 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...
|
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...
|
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...
|
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...
|
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 $
| |
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 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...
|
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{
|
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
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |