[英]I am getting errors here error is ('str' object is not callable)
print("Lets play a game")
print("You have to operte on some numbers")
print("Are you ready? , type yes or no ")
input = input().lower()
if input == "yes":
print("Ok now type your name and age (comma separated)")
name = input()
age = int(input())
if "\"" in name or ">" in name :
print("Use only alphanumeric characters")
else:
pass
if age <= 10 :
print("You cant play this game")
我不斷收到此錯誤'str' object is not callable
在第 10 行。
您正在使用input
變量隱藏內置input()
function,因此第二次調用input()
它會嘗試“調用”字符串變量。
當用戶沒有輸入您期望的內容時,您必須弄清楚該怎么做,但這里有一個代碼可能看起來像這樣的框架:
print("Let's play a game")
print("You have to operate on some numbers")
text = input("Are you ready (yes/no)?").lower()
if text == "yes":
text = input("Enter your name and age (comma separated):")
fields = text.split(",")
if len(fields) != 2:
# Handle case of bad user input format
name = fields[0].trim()
if not all(c.isalpha() or c.isspace() for c in name):
# Handle case of bad entered name
try:
age = int(fields[1])
except ValueError:
# Handle case of bad entered age
if age <= 10:
print("You cannot play this game")
您遇到的問題來自變量的名稱“輸入”。 如果您更改它,在我的示例中為“准備好”,您將避免該錯誤。
我還建議包含檢查輸入的函數並使用 while 循環,這樣如果出現意外輸入,程序就不會完成。
請看下面我的代碼:
def check_predefined_input(variable, values, input_question, error_message, check = False):
variable = str(input(input_question))
try:
if(variable.lower() in values):
check = True
else:
raise Exception ("You have inserted an invalid option.")
except Exception as e:
print("ERROR: ", e)
print(error_message, "\n")
check = False
return variable, check
def check_string_input(variable, input_question, error_message, check = False):
variable = input(input_question)
if "\"" in variable or ">" in variable:
print("ERROR: ", error_message, "\n")
check = False
else:
check = True
return variable, check
def check_int_input(variable, input_question, error_message, check = False):
try:
variable = int(input(input_question))
check = True
except ValueError as ve:
print("ERROR: ", ve)
print(error_message, "\n")
variable = 0
check = False
return variable, check
print("Lets play a game")
print("You have to operate on some numbers")
ready_values = ["yes", "no"]
ready = ''
ready_input_question = "Are you ready? , type yes or no "
ready_error_message = "\tMake sure your answer is yes or not."
ready_check = False
while not ready_check:
ready, ready_check = check_predefined_input(ready, ready_values, ready_input_question, ready_error_message, ready_check)
if ready == "yes":
name = ""
name_check = False
name_input_question = "Ok now type your name: "
name_error_message = "Use only alphanumeric characters"
while not name_check:
name, name_check = check_string_input(name, name_input_question, name_error_message, name_check)
age = 0
age_check = False
age_input_question = 'Please, ' + name + ', insert your age: '
age_error_message = '\tMake sure you insert your age.\n\tDo not insert letters.'
while not age_check:
age, age_check= check_int_input(age, age_input_question, age_error_message)
if age <= 10 :
print("You cannot play this game")
else:
print("You are ready to play")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.