Python——两个数比较大小

目标:比较两个数大小,如果a大于b,则输出a,否则提示要重新输入。

示例是用python3来运行的哈~

 

不使用函数实现:

 
  1. #coding=utf-8

  2. #coding by tanli

  3.  
  4. a=input("please input a:")

  5. b=input("please input b:")

  6.  
  7. print (a)

  8. print (b)

  9.  
  10.  
  11. if a>b:

  12. print (a)

  13. else:

  14. print ("please input again")

程序运行结果:

 
  1. please input a:5

  2. please input b:8

  3. 5

  4. 8

  5. please input again

 

使用函数来实现:

 
  1. #coding=utf-8

  2. #coding by tanli

  3.  
  4. def getResult(a,b):

  5. #定义一个命名为GetResult的方法,注意方法名首字母要小写,其后单词的首字母要大写

  6. if a > b:

  7. return a

  8. else:

  9. return "please input again"

  10.  
  11. a=input("please input a:")

  12. b=input("please input b:")

  13. print(a)

  14. print(b)

  15. print (getResult(a,b))

 


版权声明:本文为xixi20200原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。