Home > Enterprise >  How to remove certain statement from a function conditionally
How to remove certain statement from a function conditionally

Time:10-24

So let's say I have a function of

def question():
    print("")
    question_section = str("B"   str(quiz_nmbr   1))
    print("Question", str(quiz_nmbr)   ": "   str(sheet[question_section].value))
    question_become = str(input("Type your question: "))
    sheet[question_section].value = question_become 
    book.save('Quiz_Problems.xlsx')

Then let's say one time I wanted to call the question() function again.

However, I don't want print("Question", str(quiz_nmbr) ": " str(sheet[question_section].value)) to be printed.

Is there anyway to just remove that statement for certain condition? and by condition what I mean is let's say I wanted to call the function in if else statement (which condition matters to give different output)

CodePudding user response:

Try this:

def question(prompt):
    print("")
    question_section = str("B"   str(quiz_nmbr   1))
    if prompt:
        print("Question", str(quiz_nmbr)   ": "   str(sheet[question_section].value))
    question_become = str(input("Type your question: "))
    sheet[question_section].value = question_become 
    book.save('Quiz_Problems.xlsx')

Then, inside your if/else clause, you can call question(True) or question(False) as desired.

  • Related