Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Multiple IF statements
#1
so i want to be able to pick out keywords from an input and if all keywords are present i want it to output full marks!!! any help on what to do this is my code:

    breathingquestions = ['What is the equation for Respiration?', 'What do the rings of cartilage do?']                   
    if random == ('What do the rings of cartilage do?'):
                        

        answer = input('Type Answer Here:')


        if answer == ('support') and ('open') and ('breathe'):

                print('Full Marks!!!!')  
any help will be appreciated thanks!
Reply
#2
Your
if answer == ('support') and ('open') and ('breathe'):
is effectively doing
if (answer == 'support') and True and True:
or, after ditching the none needed/usefull parts.
if answer == 'support':
Based on your target result if should look like
if ('support' in answer) \
and ('open' in answer) \
and ('breathe' in answer):
(note1: The brackets '(' & ')' are not needed here and are only there for readability purposes.)
(note2: The trailing '\' character is python's line-continuation feature (not always needed, depending on the used interpreter))

Here is a bit more general-purpose/code-example that one could use/re-use.
answers = []
answers += ['foobar'] ## => True
answers += ['foo bar'] ## => True
answers += ['foo'] ## => False
answers += ['bar'] ## => False

print '-answer-','|', '-fullmarks-'

targets = ['foo','bar']
for answer in answers:
  fullmarks = True
  for target in targets:
    if target not in answer:
      fullmarks = False
  print answer,'|', fullmarks
Take note of the fact that the answer "foobar" also gets full-marks. If that's a problem I suggest digging into Python's RE(regular-expressions) feature (although that's a bit more advanced compare to this)

---

(Mmm ... Seems I missed the "Solved" status on this tread. Which seems to be not precent/indicated in the tread page itself (other than to the OP))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Multiple Loop Statements in a Variable Dexty 1 1,176 May-23-2022, 08:53 AM
Last Post: bowlofred
  Multiple Or Statements JoeDainton123 8 2,543 Sep-15-2020, 08:14 PM
Last Post: buran
  SyntaxError: multiple statements found while compiling a single statement Kayode_Odeyinka 1 2,937 Mar-12-2020, 05:50 PM
Last Post: micseydel
  optimize choices in multiple if ---: statements Pedroski55 2 2,857 Dec-25-2018, 05:06 AM
Last Post: Pedroski55
  SyntaxError: multiple statements found while compiling a single statement DragonG 1 5,398 Nov-26-2018, 05:33 AM
Last Post: Larz60+
  How to reliably split string containing multiple sqlite statements? shanepy 2 2,559 Nov-21-2018, 12:07 AM
Last Post: shanepy
  How to execute a string with multiple sqlite statements and print results? shanepy 0 2,248 Nov-20-2018, 10:19 PM
Last Post: shanepy
  Multiple "return" statements or "result" variable? WolfWayfarer 7 7,675 Jul-25-2018, 07:51 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020