Python初学者小问题

img


代码如图所示,想将第二个问题也就是那个true_false那部分改为:输入yes或者no的时候可以输出两段英文,输入其他值则提示回答无效,请问该如何修改

在true_false()函数的判断里,把执行部分里的print(calculated_value)移到if-else里面即可, 然后把if-else 改为if-elif-else ,修改如下:
参考链接:
python判断字符串相等_好生活好二三四

calculation_to_units = 24
name_of_unit = "hours"

def days_to_units(num_of_days):
    if num_of_days > 0:
        return f"{num_of_days} days are {num_of_days*calculation_to_units} {name_of_unit}"
    else:
        return ("this number is not right")

def true_false(yes_or_no,calculation_value):
    
    #https://www.how234.com/eamyrzbsh.html
    if yes_or_no == "yes" :
        print(calculation_value)
        print ("please grow up ASAP!")
    elif yes_or_no == "no":
        print(calculation_value)
        print ("all right")
    else :
        print ("Invalid answer!")
    
    


user_input = input("please enter a number!\n")


user_input2 = input("are you a little boy?\n")

#print("user_input2=",user_input2,"|")

user_input_number = int(user_input)

#user_input2_words = bool(user_input2)

calculation_value = days_to_units(user_input_number)

true_false_verification = true_false(user_input2,calculation_value)   

#print(calculation_value)

#print(true_false_verification)     

img

你这true_false函数不对啊,参数都没有使用啊,应该是

def true_false(yes_or_no):
  if yes_or_no == "yes" :
      return "Please grow up ASAP!"
  elif yes_or_no == "No":
     return "All right"
  else:
     return "wrong answer"


def true_false(x):
    if x == 'yes':
        print('yes')
    elif x == 'no':
        print('no')
    else:
        print('回答无效')

x = input('请输入你的回答:')
true_false(x)

def true_false(yes_or_no):
if yes_or_no == 'yes':
return ("please grow up ASAP!")
elif yes_or_no == 'no':
return ("all rigth!")
else:
return ("Invalid answer!")