Skip to main content
Code Review

Return to Answer

added 355 characters in body
Source Link
jsanc623
  • 2.9k
  • 16
  • 22

produces these responses (output → expected_output):

False → **False**
True → **True**
True → **True**
True → **False**
True → **True**
True → **False**
True → **True**
False → **False**
False → **False**
True → **True**
Asdf → **False**

This is the output of my version of the func (using the same calls and expected output as above in the original call) (output → expected_output):

False → **False**
True → **True**
True → **True**
False → **False**
True → **True**
False → **False**
True → **True**
False → **False**
False → **False**
True → **True**
False → **False**

produces these responses:

False
True
True
True
True
True
True
False
False
True
Asdf

This is the output of my version of the func (using the same calls and expected output as above in the original call)

False
True
True
False
True
False
True
False
False
True
False

produces these responses (output → expected_output):

False → **False**
True → **True**
True → **True**
True → **False**
True → **True**
True → **False**
True → **True**
False → **False**
False → **False**
True → **True**
Asdf → **False**

This is the output of my version of the func (using the same calls and expected output as above in the original call) (output → expected_output):

False → **False**
True → **True**
True → **True**
False → **False**
True → **True**
False → **False**
True → **True**
False → **False**
False → **False**
True → **True**
False → **False**
Source Link
jsanc623
  • 2.9k
  • 16
  • 22

It seems that your original code does not work correctly, as taking your code:

def in1to10(n, outside_mode):
 if(n>1 and n<10):
 return not outside_mode
 else:
 return outside_mode or n is 10 or n is 1

and calling it in this manner, with the expected output in the comment next to the func call:

print in1to10(-1, False) # → **False**
print in1to10(-1, True) # → **True**
print in1to10(1, False) # → **True**
print in1to10(1, True) # → **False**
print in1to10(10, False) # → **True**
print in1to10(10, True) # → **False**
print in1to10(5, False) # → **True**
print in1to10(5, True) # → **False**
print in1to10(11, False) # → **False**
print in1to10(11, True) # → **True**
print in1to10("adsf", "Asdf") # False

produces these responses:

False
True
True
True
True
True
True
False
False
True
Asdf

Notice that your original answer does not catch nor raise an error when encountering a string val.

I've reworked your code to this:

def in1to10(n, outside_mode=False):
 retval = True if outside_mode == False else False
 return retval if 1 <= n <= 10 else not retval

which does the following:

  1. It defaults outside_mode to False, so that we can omit it and only give it a number to check if its within the range
  2. Checks if n is within a range elegantly
  3. Uses the retval var as a flip switch boolean
  4. Handles strings and outputs False if its a string
  5. Correctly outputs the expected output

This is the output of my version of the func (using the same calls and expected output as above in the original call)

False
True
True
False
True
False
True
False
False
True
False
lang-py

AltStyle によって変換されたページ (->オリジナル) /