[英]Traceback error when using loop
The below code is written in python. 下面的代码是用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()
Whenever I run the file in the CMD Shell, it works fine, until I get to this part of the code: 每当我在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
If user_input doesn't return as 1 or 2, I get this error: 如果user_input不返回1或2,则会出现此错误:
I can't exactly figure out what is wrong with my code, and I don't understand any of the errors thrown by the CMD Shell. 我无法确切地弄清楚我的代码出了什么问题,而且我也不了解CMD Shell抛出的任何错误。 Does anyone know what I'm doing wrong?
有人知道我在做什么错吗?
The problem is here: 问题在这里:
user_input = int(input('Please type 1 or 2: '))
At this point, you set user_input
to a number, and then later on you are trying to call that number with user_input()
, its the same as doing this: 此时,您将
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.