执行程序python时出错

时间:2017-09-11 01:39:12

标签: python-3.x

def main():
   get_value =get_display_info('5 6 0','7 0 0 0 5')
   print(get_value)

def get_display_info(dice_to_roll_again_str, dice_set_aside_str):   
   length1 =len(dice_to_roll_again_str)
   d = dice_set_aside_str
   if dice_set_aside_str == None:
     return 0
   else :
     return len(dice_set_aside_str)
   if length1 and dice_set_aside_str  > 0:
     return "(Dice to roll again:" + str(dice_to_roll_again_str) +','+ 
              "Dice set aside:" + str(d) + ')'
   elif length1 > 0:
     return "(Dice to roll again:" + str(dice_to_roll_again_str) + ')'
   elif dice_set_aside_str > 0:
     return "(Dice set aside:" + str(d) + ')'

为什么程序在达到此声明后会停止执行?

if length1 and dice_set_aside_str > 0:

2 个答案:

答案 0 :(得分:0)

您的代码将在此块之后无条件返回:

def get_display_info(dice_to_roll_again_str, dice_set_aside_str):   
   length1 =len(dice_to_roll_again_str)
   d = dice_set_aside_str
   if dice_set_aside_str == None:
     return 0
   else :
     return len(dice_set_aside_str)

答案 1 :(得分:0)

Hello Adrian,

解决方案

请尝试以下代码,

def main():
    get_value =get_display_info('5 6 0','7 0 0 0 5')
    print "Value : ",get_value

def get_display_info(dice_to_roll_again_str, dice_set_aside_str):   

    length1 = len(dice_to_roll_again_str)
    d = dice_set_aside_str

    if dice_set_aside_str == "":
     return 0
    else :
     return len(dice_set_aside_str)

    if length1 and dice_set_aside_str  > 0:
     return "(Dice to roll again:{}".format(str(dice_to_roll_again_str))+",Dice set aside:{}".format(str(dice_set_aside_str))+")"
    elif length1 > 0:
     return "(Dice to roll again:{}".format(str(dice_to_roll_again_str))+")"
    elif dice_set_aside_str > 0:
     return "Dice set aside:{}".format(str(dice_set_aside_str))+")"

# Call the main() function
main()
# Call and Print get_display_info() function.
print "Display information : ",get_display_info("mayur","vora")

我希望我的回答很有帮助 如果有任何疑问请评论。