Hello,
I have a “TypeError: ‘str’ object is not callable” in the block who define the function “again” when the else is executed, and I can’t figure out to solve it. Why this call doesn’t work
"""
Codacademy project
User choose between a game of coin or dice, then try to guess the roll,
program will tell him if he wins or not
https://discuss.codecademy.com/t/computer-science-independent-project-1-coin-flip-python/474480
"""
from random import randint, choice
def want_to_play(name):
"""
function to ask if the user want to play,
if yes he has to choose the game he wants to play
parameter: user's name
return: user's game choice
"""
ready = input("Hello! " + str(name) +", are you ready to play? Type Yes or No " )
ready.lower()
if ready == "yes":
game = input("Which game do you want to play? Dice or coin flip? Type Dice or Coinflip ")
game = game.lower()
game_choice(game)
elif ready == "no":
print("Fine, I will find someone else")
return
else:
print("I didn't expect this answer")
want_to_play(name)
def game_choice(game):
"""
function to launch the appropriate game the the user's choice
parameter: user's game choice
return: none
"""
total_count = 0
successfull_count = 0
if game == "dice":
dice_roll(game, total_count, successfull_count)
elif game == "coinflip":
coin_flip(game, total_count, successfull_count)
else:
print("It looks like you didn't type the game you want to play")
want_to_play("manu")
def coin_flip(game, total_count, successfull_count):
"""
function who randomly choose a number/face coin and inform the user if he wins
or not, plus historic of all the succes guesses/guesses
parameter: total_count, successfull_count
return: none
"""
user_guess = input("Pile ou face?")
user_guess.lower()
coin_result = choice(["pile", "face"])
if user_guess != "pile" and user_guess != "face":
print("I expect pile or face")
coin_flip(total_count, successfull_count)
else:
if user_guess == coin_result:
print("####### \O/ #######")
print("You won!, the result of the flip coin is {}".format(coin_result))
print("#############################")
total_count += 1
successfull_count += 1
print("You total_guess: "+str(total_count))
print("Your succesfull_guess: "+str(successfull_count))
else:
print("####### /O\ #######")
print("You lost!, the result of the flip coin is {}".format(coin_result))
print("#############################")
total_count += 1
print("Your total_guess: "+str(total_count))
print("Your succesfull_guess: "+str(successfull_count))
print("Try again!")
again(game, total_count, successfull_count)
def dice_roll(game, total_count, successfull_count):
"""
function who randomly choose a number/face coin and inform the user if he wins
or not, plus historic of all the succes guesses/guesses
parameter: none
return: none
"""
user_guess = input("Which number between 1 and 6 do you choose? ")
user_guess = int(user_guess)
if user_guess > 6:
print("you entered a number bigger than 6 ")
dice_roll(total_count, successfull_count)
elif user_guess < 1:
print("you entered a number lower than 1 ")
dice_roll(total_count, successfull_count)
dice_result = choice(list(range(1, 7)))
if user_guess == dice_result:
print("####### \O/ #######")
print("You won!, the result of the diceroll is {}".format(dice_result))
print("#############################")
successfull_count += 1
total_count += 1
print("Your total_guess: "+str(total_count))
print("Your successfull_guess: "+str(successfull_count))
else:
print("####### /O\ #######")
print("You lost!, the result of dice roll is {}".format(dice_result))
print("#############################")
total_count += 1
print("Your total_guess: "+str(total_count))
print("Your successfull_guess: "+str(successfull_count))
print("Try again!")
again(game, total_count, successfull_count)
def again(game, total_count, successfull_count):
again = input("Do you want to continue to play? Type Yes or No: ")
again.lower()
if again == "yes" and game == "coinflip":
coin_flip(game, total_count, successfull_count)
elif again == "no" and game == "coinflip":
print("Ok! Goodbye")
elif again == "yes" and game == "dice":
dice_roll(game, total_count, successfull_count)
elif again == "no" and game == "dice":
print("Ok! Goodbye")
else:
print("I expect Yes or No")
again(game, total_count, successfull_count)
want_to_play("Bob")