[英]Traceback error when using loop
下面的代码是用python编写的。
import subprocess as sp
def user_input():
sp.call('cls',shell=True)
print('[1]: ASCII to Binary')
print('[2]: Binary to ASCII')
valid_answer()
def valid_answer():
while True:
try:
user_input = int(input('Please type 1 or 2: '))
sp.call('cls',shell=True)
break
except ValueError:
print('Please enter a valid input...')
sp.call('pause',shell=True)
user_input()
while True:
if user_input < 0:
print('Input must be 1 or 2.')
sp.call('pause',shell=True)
user_input()
elif user_input > 2:
print('Input must be 1 or 2.')
sp.call('pause',shell=True)
user_input()
else:
break
if user_input == (1):
print('Good.')
exit
elif user_input == (2):
print('Good.')
exit
user_input()
每当我在CMD Shell中运行该文件时,它都可以正常工作,直到获得代码的这一部分:
while True:
if user_input < 0:
print('Input must be 1 or 2.')
sp.call('pause',shell=True)
user_input()
elif user_input > 2:
print('Input must be 1 or 2.')
sp.call('pause',shell=True)
user_input()
else:
break
如果user_input不返回1或2,则会出现此错误:
我无法确切地弄清楚我的代码出了什么问题,而且我也不了解CMD Shell抛出的任何错误。 有人知道我在做什么错吗?
问题在这里:
user_input = int(input('Please type 1 or 2: '))
此时,您将user_input
设置为一个数字,然后稍后尝试使用user_input()
调用该数字,其方法与此相同:
>>> user_input = 1
>>> user_input()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.