我是一名新手程序员。我在使用Python编写代码(使用def函数)时遇到了一些问题。
基本上,该代码需要从两个数字中输出较小的数字。
问题: 编写代码以执行以下任务:
- 定义一个名为smaller_num的函数,它接受两个数字来确定并返回其中较小的数字。
询问用户输入两个数字
使用该函数确定较小的数字并显示结果
所以我使用了用户输入而不是调用函数并将值添加到变量中。
这是我代码的样子:
def smaller_num(x,y):
if x>y:
number= y
else:
number= x
return number
smaller_num(x= input("Enter first number:-") ,y= input("Enter second number:-"))
print("The smaller number between " + str(x) + " and " + str(y) + " is " + str(smaller_num))
我该如何纠正它?目前它无法工作,因为“x”未定义。但我感觉我在def函数和input函数中都清楚地定义了它们。那么我该如何解决这个问题呢?
感谢回答这个问题的人。