Python-未定义Num1 1

Python-未定义Num1 1,python,Python,我想知道我是否能正确编码。收到的num1未定义错误 您需要将函数中的num1返回到\u numbers(否则,您不能使用大于到\u numbers的值)。然后您需要调用来定义num1,如下所示: def to_numbers(): num1 = str(raw_input('Enter a string number?\n')) for value in num1: try: print '%s as an int is %d' % (str(value), int(value

我想知道我是否能正确编码。收到的
num1未定义
错误

您需要将函数
中的
num1
返回到\u numbers
(否则,您不能使用大于
到\u numbers
的值)。然后您需要调用
来定义
num1
,如下所示:

def to_numbers():
  num1 = str(raw_input('Enter a string number?\n'))

for value in num1:
  try:
    print '%s as an int is %d' % (str(value), int(value))
  except ValueError, ex:
    print '"%s" cannot be converted to an int: %s' % (value, ex)

您需要将函数
中的
num1
返回到\u numbers
(否则,您不能使用大于
到\u numbers
)的值。然后您需要调用
来定义
num1
,如下所示:

def to_numbers():
  num1 = str(raw_input('Enter a string number?\n'))

for value in num1:
  try:
    print '%s as an int is %d' % (str(value), int(value))
  except ValueError, ex:
    print '"%s" cannot be converted to an int: %s' % (value, ex)

您必须插入选项卡,以便for位于函数内部。否则,您只需要有一个函数,该函数读取一个值,并在其外部为
读取一个
,该函数无法访问
num1

,您必须插入制表符,以便for位于函数内部。否则,您只需要有一个函数,该函数读取一个值和一个外部的
,该函数无法访问
num1
,Python对缩进非常敏感,因此请确保正确缩进代码以获得预期的结果。由于num1范围在方法to_numbers()中,而for循环不在to_numbers()范围中,因此出现错误,所以将num1作为未定义错误

删除num1赋值和for循环之间的行,缩进for循环使其同步(空格),就像num1将使num1在to_numbers()中可访问一样

当我使用Python3.x时,我必须更新一些语法,比如在打印语句中添加括号,以及使用As关键字来命名异常实例,但您的逻辑保持不变

def to_numbers():
    num1 = str(raw_input('Enter a string number?\n'))
    return num1

num1=to_numbers()
for value in num1:
    try:
        print '%s as an int is %d' % (str(value), int(value))
    except ValueError, ex:
        print '"%s" cannot be converted to an int: %s' % (value, ex)
样本运行

import platform

## Your method
def to_numbers():
  num1 = str(input('Enter a string number?\n'))
  for value in num1:
    try:
      print ('%s as an int is %d' % (str(value), int(value)))
    except ValueError as ex :
      print ('"%s" cannot be converted to an int: %s' % (value, ex))

## Print Python version for your reference
print("Python version : " + platform.python_version())
## Calling your method
to_numbers()

Python对缩进非常敏感,因此请确保正确缩进代码以获得预期结果。由于num1范围在方法to_numbers()中,而for循环不在to_numbers()范围中,因此出现错误,所以将num1作为未定义错误

删除num1赋值和for循环之间的行,缩进for循环使其同步(空格),就像num1将使num1在to_numbers()中可访问一样

当我使用Python3.x时,我必须更新一些语法,比如在打印语句中添加括号,以及使用As关键字来命名异常实例,但您的逻辑保持不变

def to_numbers():
    num1 = str(raw_input('Enter a string number?\n'))
    return num1

num1=to_numbers()
for value in num1:
    try:
        print '%s as an int is %d' % (str(value), int(value))
    except ValueError, ex:
        print '"%s" cannot be converted to an int: %s' % (value, ex)
样本运行

import platform

## Your method
def to_numbers():
  num1 = str(input('Enter a string number?\n'))
  for value in num1:
    try:
      print ('%s as an int is %d' % (str(value), int(value)))
    except ValueError as ex :
      print ('"%s" cannot be converted to an int: %s' % (value, ex))

## Print Python version for your reference
print("Python version : " + platform.python_version())
## Calling your method
to_numbers()

你从不调用你的函数。此外,
num1
是函数的本地值,您永远不会返回值……请注意indentation@Stratton-你试过我的解决方案吗?如果成功了,请接受答案并投票支持!你从不调用你的函数。此外,
num1
是函数的本地值,您永远不会返回值……请注意indentation@Stratton-你试过我的解决方案吗?如果成功了,请接受答案并投票支持!