NameError:name'??'没有定义

问题描述 投票:2回答:1

我有一个简单的代码如下。

finished = True

def number():
    x = int(input("Please enter a number \n"))
    m = x%2

if m>0:
    print("Odd")
    finished = True
else: 
    print("Even")
    finished = True

while finished:
    number()

当我运行代码时,它显示一个问题

at line 6: if m > 0:
NameError: name 'm' is not defined. 

我想知道如何解决这个问题?

python-3.x nameerror
1个回答
2
投票

只需在函数中移动if语句即可。

def number():
    x = int(input("Please enter a number \n"))
    m = x%2
    if m>0:
        print("Odd")
        finished = True
    else: 
        print("Even")
        finished = True
© www.soinside.com 2019 - 2024. All rights reserved.