Python系列-比较两个数的大小

来源:互联网 发布:杀人软件 编辑:程序博客网 时间:2024/06/05 16:41

目标:比较两个数大小,如果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后面没有加括号


原创粉丝点击