提示用户输入 2 个数字,并确定哪个更小。但必须使用自定义函数完成,而不是内置"max"函数



>我已经将代码编辑为以下内容,并获得了更有利的结果。

#Python program that will prompt user to enter two numbers, one at a time 
and then determine which of the two numbers is smaller.
#define functions
def find_min(first, second):
if first < second:
return first
else:
#second < first
return second
#Get user input
first = int(input('Please type first number: '))
second = int(input('Please type second number: '))
def print_result(first, second):
if first < second:
print(first, 'is smaller than' ,second)
else:
#second < first
print(second, 'is smaller than' ,first)
def main():
#Get user input
first = int(input('Please type first number: '))
second = int(input('Please type second number: '))

print(print_result(first, second))
main()

__ 我现在正在让代码工作,但它在结果末尾打印"无"。函数"print_result"现在没有输入字符串"小于"。

我在你的代码中看到两个问题:

  1. 如上所述,您不会向 find_min 函数传递任何变量
  2. 您不返回值,而是打印答案

最后一行应如下所示:

print("smallest number is: {}".format(find_min(a,b))

最新更新