如何用PYTHON判断奇偶数

 时间:2026-02-16 21:59:36

1、首先我们打开JUPYTER NOTEBOOK,新建一个空白的PYTHON文档。当然也可以用其它编译器的。

如何用PYTHON判断奇偶数

2、n = input("Please input a number: ")

if n % 2 == 0:

    print("This is an even.")

elif n % 2 != 0:

    print("This is an odd")

我们首先定义一个变量n,然后用INPUT问用户输入变量。

判断是否奇偶数可以用余数来作为判断依据。但是这里有点问题。

如何用PYTHON判断奇偶数

3、n = int(input("Please input a number: "))

if n % 2 == 0:

    print("This is an even.")

elif n % 2 != 0:

    print("This is an odd")

我们要注意要定义输入的变量是整型,因为字符串是不能进行运算的。

如何用PYTHON判断奇偶数

4、n = float(input("Please input a number: "))

if n % 2 == 0:

    print("This is an even.")

elif n % 2 != 0:

    print("This is an odd")

有些情况下我们需要用到浮点型,就是float,注意两者的区别。

如何用PYTHON判断奇偶数

5、n = int(input("Please input a number: "))

if n % 4 == 0:

    print("This number is a multiple of 4.")

elif n % 4 != 0:

    print("This number is not a multiple of 4")

除了除以2以外,还可以除以4来作为判断,主要看自己想要判断什么数字。

如何用PYTHON判断奇偶数

6、num1 = int(input("Please input a number: "))

num2 = int(input("Please input a number: "))

if num1 % num2 == 0:

    print("This is OK.")

elif num1 % num2 != 0:

    print("This is NOT OK")

换一种方法,输入两种数字,不止可以判断是否奇偶数以外,还可以判断是否两个数字是否整除。

如何用PYTHON判断奇偶数

  • Python编程:怎么判断一个数字是否为奇数或偶数
  • 如何用python判断一个整数是奇数或偶数
  • 如何用C语言求水仙花数
  • C语言编程:输入三个数取最大值/三个数求最大值
  • 洛必达公式是什么
  • 热门搜索
    学好英语的方法 加油图片大全励志图片 男生瘦脸的最快方法 工资证明怎么写 wps怎么设置页码 入党志愿书怎么写 琉璃神社怎么下载 玻璃体混浊的治疗方法 家常素菜100道 禁止标志图片大全