0

I want to know if its possible to put two variables into one if statement, all in one block for each level including current_text variable and answers variable. I want to make the code shorter it has too many "if". For example to put:

if level in easy:
     current_text = easy_level
     answers = easy_answers   

This is the code:

    if level in easy:
        current_text = easy_level
    if level in medium:
        current_text = medium_level
    if level in hard:
        current_text = hard_level
    if level in easy:
        answers = easy_answers
    if level in medium:
        answers = medium_answers
    if level in hard:
        answers = hard_answers
    ask_questions(current_text, answers)

Thanks

3
  • 3
    Why don't you try and see ? ;) Commented Oct 1, 2015 at 4:30
  • What's the problem? You're checking the same conditions multiple times, why not just put the statements together exactly like you show in your first example. What have you tried that isn't working? Commented Oct 1, 2015 at 4:31
  • See also: sourcemaking.com/refactoring/…, and maybe this discussion: c2.com/cgi/wiki?SwitchStatementsSmell Commented Oct 1, 2015 at 4:40

3 Answers 3

2

Yes you can do something like below code.

print "Please enter a number between 1 and 20"
enter_num = int(raw_input("> "))

if enter_num >= 1 and enter_num <= 20: 
    print "You have entered a valid number"
    if enter_num % 2 == 0: #test for even/odd
             print "Your number is even"
             print enter_num * enter_num
    elif enter_num % 2 == 1: #test for even/odd
             print "Your number is odd"
             print enter_num * 3
 else:
    print "You've entered an invalid number"

The detailed explanation can be found here.

https://blog.udemy.com/python-if-else/

Your final if condition can be something like below.

if level in easy:
    current_text = easy_level
    answers = easy_answers
if level in medium:
    current_text = medium_level
    answers = medium_answers
if level in hard:
    current_text = hard_level
    answers = hard_answers
ask_questions(current_text, answers)
Sign up to request clarification or add additional context in comments.

Comments

1

Short answer, yes.

if level in easy:
    current_text = easy_level
    answers = easy_answers
elif level in medium:
    current_text = medium_level
    answers = medium_answers
elif level in hard:
    current_text = hard_level
    answers = hard_answers
ask_questions(current_text, answers)

1 Comment

@EduardoGonzalez Yes, as long as your code is indented correctly you can have as many lines in an if statement as you'd like. Check the link in J-D's answer for more details.
0

This looks like a case where you might prefer to use dictionary dispatch:

current_text = text[level]
current_answers = answers[level]

... where level is set to something in ('easy', 'medium', 'hard') ... or whatever.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.