Reputation: 13
I want to execute my def function in a way if it encounters a None in its parameter it will return a blank space or else it will measure its length.
def get_display_info(dice_to_roll_again_str, dice_set_aside_str):
length1 =len(dice_to_roll_again_str)
if dice_set_aside_str == None:
return ' '
else :
length2 =len(dice_set_aside_str)
if length2 != None:
if length1 and length2 > 0:
return "(Dice to roll again:" + str(dice_to_roll_again_str) +','+ "Dice set aside:" + str(dice_set_aside_str) + ')'
elif length1 > 0:
return "(Dice to roll again:" + str(dice_to_roll_again_str) + ')'
elif length2 > 0:
return "(Dice set aside:" + str(dice_set_aside_str) + ')'
Upvotes: 0
Views: 105
Reputation: 962
1. What is the null or None Keyword
http://pythoncentral.io/python-null-equivalent-none/
2. format() function
https://pyformat.info/
3. Python Learning for Begniner
- https://www.tutorialspoint.com/python/ (Beginer For Best)
- https://www.learnpython.org/
- https://www.javatpoint.com/python-tutorial
- https://docs.python.org/3/tutorial/
- https://www.python.org/dev/peps/pep-0008/ (Coding Stadered)
- https://docs.python.org/2/library/functions.html (Python InBuilt Function)
When ever check the string length so not compare with None but compare with null, "", '', isEmpty()...etc
various function.
You are write all the condition false not any condition proper so write proper condition and use coding standard so make more powerful program more reference for read python documentation.
I give solution but not change the all condtion because i don't know what your project and what you write.
def get_display_info(dice_to_roll_again_str, dice_set_aside_str):
if dice_set_aside_str == '':
return ' '
else :
length2 =len(dice_set_aside_str)
if dice_to_roll_again_str == '':
return ' '
else :
length1 =len(dice_to_roll_again_str)
if length2 != 0 and length1!=0:
if length1==1 and length2==1:
return "(Dice to roll again:{}".format(str(dice_to_roll_again_str))+",Dice set aside:{}".format(str(dice_set_aside_str))+")"
elif length1 > 1:
return "(Dice to roll again:{}".format(str(dice_to_roll_again_str))+")"
elif length2 > 1:
return "(Dice set aside:{}".format(str(dice_set_aside_str))+")"
print get_display_info("vora","m")
Upvotes: 0
Reputation:
None, True and False tests are done with words:
if dice_set_aside_str is not None:
return 0
Anything iterable without any items resolves to False
in boolean tests. So do empty strings and so does None. This we can combine:
if not dice_set_aside_str:
return 0
else:
return len(dice_set_aside_str)
Upvotes: 1