您的位置:首页 > 编程语言 > Python开发

Python比较两个数的大小

2017-10-29 21:17 316 查看
目标:比较两个数大小,如果num1大于num2,则输出num1>num2,否则将输出2

import random

def compareNum(num1,num2):

        if(num1 > num2):

            return str(num1)+">"+str(num2)

        else:

            return 2

num1 = random.randrange(1,5)

num2 = random.randrange(1,3)

input ("num1 = "), num1

input ("num2 = "), num2

print (compareNum(num1,num2))

过程中容易出现的问题和解决方法:

C:\Users\tl>python C:\Users\tl\Desktop\test.py

  File "C:\Users\tl\Desktop\test.py", line 7

    num1 = 4

           ^

1、TabError: inconsistent use of tabs and spaces in indentation

直接定义值的方式不正确

C:\Users\tl>python C:\Users\tl\Desktop\test.py

  File "C:\Users\tl\Desktop\test.py", line 11

    print compareNum

                   ^

2、IndentationError: unexpected indent

该行缩进不正确

C:\Users\tl>python C:\Users\tl\Desktop\test.py

  File "C:\Users\tl\Desktop\test.py", line 11

    print compareNum

                   ^

3、SyntaxError: Missing parentheses in call to 'print'. Did you mean print(compareN

um)?

Print后面没有加括号
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: