#Library required
import random
#Introduction and display of options
print("Made by cn1Qu9n.\n")
print(
"Easy: 3 tries to guess numbers 0 to 10.\nHard: 5 tries to guess numbers 0 to 20.\n"
)
#Variables
Easy_List = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Hard_List = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
]
Easy = str(random.choice(Easy_List))
Hard = str(random.choice(Hard_List))
Answer = 0
#Definitions of functions
def Level():
Level = input("Difficulty (Easy/Hard): ")
if Level == "Easy":
Easy_Level()
elif Level == "Hard":
Hard_Level()
else:
print(
"Invalid input. Only \"Yes\" or \"No\" is allowed (Case sensitive). Please try again."
)
Level()
def Easy_Level():
Tries = 0
while Tries < 3:
Tries = Tries 1
Answer = input("Enter your guess: ")
if Answer < Easy:
print("The number is higher!")
elif Answer > Easy:
print("The number is lower!")
elif Answer == Easy:
print("You won!\n")
Resume()
else:
print("You lost!")
Resume()
def Hard_Level():
Tries = 0
while Tries < 5:
Tries = Tries 1
Answer = input("Enter your guess: ")
if Answer < Hard:
print("The number is higher!")
elif Answer > Hard:
print("The number is lower!")
elif Answer == Hard:
print("You won!\n")
Resume()
else:
print("You lost!")
Resume()
#To determine whether to resume
def Resume():
Resume = input("Do you want to continue playing (Yes/No)? ")
print("\n")
if Resume == "Yes":
Level()
elif Resume == "No":
print("Exiting...")
exit()
else:
print("Invalid input. Only \"Yes\" or \"No\" is allowed (Case sensitive). Please try again.")
Resume()
#Start of functions
Level()
After searching online, I found that the error occurs when you try to call a string as a function and can be resolved by changing the module name. However, even after changing the module name many times, the error continues to occur in module Resume() in the "else" when running program. Unable to resolve the error even after changing the function name. Please advise.
CodePudding user response:
This is because in the Resume
function you defined a variable named Resume
:
def Resume():
# They have the same name!
Resume = input("Do you want to continue playing (Yes/No)? ")
So, once you define that variable, Python thinks that the name of variable "Resume" is referring to that string. After that, in your else
section, you're using that name as a callable, just like a function, but turns out its a string now:
else:
print("Invalid input. Only \"Yes\" or \"No\" is allowed (Case sensitive). Please try again.")
Resume() # This is actually a string!
You should rename that variable to avoid conflicts with the original function name.
A possible solution (rename user input variable to keep_playing
):
#To determine whether to resume
def Resume():
keep_playing = input("Do you want to continue playing (Yes/No)? ")
print("\n")
if keep_playing == "Yes":
Level()
elif keep_playing == "No":
print("Exiting...")
exit()
else:
print("Invalid input. Only \"Yes\" or \"No\" is allowed (Case sensitive). Please try again.")
Resume()
CodePudding user response:
I ran your code and was able to get the same error TypeError: 'str' object is not callable
In my case, I saw that the call tot he Level() function was indented when it should not be indeed. As it is, you are calling the level function within the Level() function itself, as if it is a recursive function.
I simply moved the call to the Level() outside the function.
Also, I noticed that after moving the Level() function, you are calling that function twice, once right after the Level() and once in the last line of code.
#Library required
import random
#Introduction and display of options
print("Made by cn1Qu9n.\n")
print(
"Easy: 3 tries to guess numbers 0 to 10.\nHard: 5 tries to guess numbers 0 to 20.\n"
)
#Variables
Easy_List = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Hard_List = [
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20
]
Easy = str(random.choice(Easy_List))
Hard = str(random.choice(Hard_List))
Answer = 0
#Definitions of functions
def Level():
print("Level()")
Level = input("Difficulty (Easy/Hard): ")
if Level == "Easy":
Easy_Level()
elif Level == "Hard":
Hard_Level()
else:
print(
"Invalid input. Only \"Yes\" or \"No\" is allowed (Case sensitive). Please try again."
)
Level()
def Easy_Level():
print("Easy_Level()")
Tries = 0
while Tries < 3:
Tries = Tries 1
Answer = input("Enter your guess: ")
if Answer < Easy:
print("The number is higher!")
elif Answer > Easy:
print("The number is lower!")
elif Answer == Easy:
print("You won!\n")
Resume()
else:
print("You lost!")
Resume()
def Hard_Level():
print("Hard_Level()")
Tries = 0
while Tries < 5:
Tries = Tries 1
Answer = input("Enter your guess: ")
if Answer < Hard:
print("The number is higher!")
elif Answer > Hard:
print("The number is lower!")
elif Answer == Hard:
print("You won!\n")
Resume()
else:
print("You lost!")
Resume()
#To determine whether to resume
def Resume():
print("Resume()")
Resume = input("Do you want to continue playing (Yes/No)? ")
print("\n")
if Resume == "Yes":
Level()
elif Resume == "No":
print("Exiting...")
exit()
else:
print("Invalid input. Only \"Yes\" or \"No\" is allowed (Case sensitive). Please try again.")
Resume()
#Start of functions
Level()
Hope this was the fix you were hoping for.