Destiny
Destiny

Reputation: 65

Why is my function executing twice?

A function in my code repeats twice. In the chooseName() function, it asks the question, and once you answer, it repeats the question and then moves on with the rest of the code. Why does it do this?

# -*- coding: utf-8 -*-
from __future__ import print_function
import random
import time

def displayIntro():
    print("Hello, there! Glad to meet you!")
    print("Welcome to the world of Pokémon!")
    print("My name is Maple.")
    print("People affectionately refer to me as the Pokémon Professor.")
    print("This world is inhabited far and wide by creatures called Pokémon.")
    print("For some people, Pokémon are pets.")
    print("Others use them for battling.")
    print("As for myself, I study Pokémon as a profession.")
    print("But first, tell me a little about yourself.")

def chooseGender():
    gender = ""
    while gender != "boy" and gender != "girl":
        gender = raw_input("Now tell me. Are you a boy? Or are you a girl? ")

    return gender

def chooseName():
    name = ""
    name = raw_input("Let's begin with your name. What is it? ")

    return name

def nameConfirmation():
    name = chooseName()
    answer = ""
    while answer != "yes" and answer != "no":
        answer = raw_input("Right... So your name is " + str(name) + "? (yes or no) ")

    return answer
    if answer == "yes":
        print("I have a grandson.")
        print("He's been your rival since you both were babies.")
        print("...Erm, what was his name now?")
        # raw_input for their name
        print("...Er, was it") #raw_input for name
        # Let user pick yes or no
        # If yes, move on
        # If no, ask name again
        print("That's right! I remember now! His name is") #raw_input for name
        print("your name!") # raw_input for name
        print("Your very own Pokémon legend is about to unfold!")
        print("A world of dreams and adventures with Pokémon awaits! Let's go!")
    if answer == "no":
        chooseName()

displayIntro()
chooseGender()
chooseName()
nameConfirmation()

I apologize for not posting the rest of the code sooner. This is the output.

Let's begin with your name. What is it? Raven
Let's begin with your name. What is it? Raven

Upvotes: 0

Views: 1877

Answers (4)

Liora Haydont
Liora Haydont

Reputation: 1283

You call the function choseName() and then right after you call nameConfiguration() wich starts by calling choseName(), that's why it is called twice. You can simply remove the choseName() before nameConfiguration().

Upvotes: 0

imam ali
imam ali

Reputation: 26

Remove the chooseName( ) call below chooseGender( ) as it has already been called in nameConfirmation( ) definition. It worked for me.

Upvotes: 1

Mahesh Karia
Mahesh Karia

Reputation: 2055

As chooseName() is getting called twice first below call of chooseGender() and second inside nameConfirmation() so that's the reason it is getting executed twice. As a fixed you can comment or remove chooseName() which is below chooseGender() as shown.

displayIntro()
chooseGender()
# chooseName()
nameConfirmation()

So updated code will be as follows:

# -*- coding: utf-8 -*-
from __future__ import print_function
import random
import time

def displayIntro():
    print("Hello, there! Glad to meet you!")
    print("Welcome to the world of Pokémon!")
    print("My name is Maple.")
    print("People affectionately refer to me as the Pokémon Professor.")
    print("This world is inhabited far and wide by creatures called Pokémon.")
    print("For some people, Pokémon are pets.")
    print("Others use them for battling.")
    print("As for myself, I study Pokémon as a profession.")
    print("But first, tell me a little about yourself.")

def chooseGender():
    gender = ""
    while gender != "boy" and gender != "girl":
        gender = raw_input("Now tell me. Are you a boy? Or are you a girl? ")

    return gender

def chooseName():
    name = ""
    name = raw_input("Let's begin with your name. What is it? ")

    return name

def nameConfirmation():
    name = chooseName()
    answer = ""
    while answer != "yes" and answer != "no":
        answer = raw_input("Right... So your name is " + str(name) + "? (yes or no) ")

    return answer
    if answer == "yes":
        print("I have a grandson.")
        print("He's been your rival since you both were babies.")
        print("...Erm, what was his name now?")
        # raw_input for their name
        print("...Er, was it") #raw_input for name
        # Let user pick yes or no
        # If yes, move on
        # If no, ask name again
        print("That's right! I remember now! His name is") #raw_input for name
        print("your name!") # raw_input for name
        print("Your very own Pokémon legend is about to unfold!")
        print("A world of dreams and adventures with Pokémon awaits! Let's go!")
    if answer == "no":
        chooseName()

displayIntro()
chooseGender()
# chooseName()
nameConfirmation()

Upvotes: 0

Aaditya Ura
Aaditya Ura

Reputation: 12689

Some modification in your code , Here is updated code:

First edit :

You are calling chooseName() two times ,

Second edit :

You are returning before main logic of your program.

Here

def nameConfirmation():
    name = chooseName()
    answer = ""
    while answer != "yes" and answer != "no":
        answer = raw_input("Right... So your name is " + str(name) + "? (yes or no) ")

    return answer

You should keep in mind that function is not going to execute anything after you return from it so your code from if answer == "yes": is not going to execute even if user type yes or no

So return it at the last of the program or if you want to return at the same place then use print there instead of 'return'.

# -*- coding: utf-8 -*-
from __future__ import print_function
import random
import time

def displayIntro():
    print("Hello, there! Glad to meet you!")
    print("Welcome to the world of Pokémon!")
    print("My name is Maple.")
    print("People affectionately refer to me as the Pokémon Professor.")
    print("This world is inhabited far and wide by creatures called Pokémon.")
    print("For some people, Pokémon are pets.")
    print("Others use them for battling.")
    print("As for myself, I study Pokémon as a profession.")
    print("But first, tell me a little about yourself.")

def chooseGender():
    gender = ""
    while gender != "boy" and gender != "girl":
        gender = input("Now tell me. Are you a boy? Or are you a girl? ")

    return gender

def chooseName():
    name = ""
    name = input("Let's begin with your name. What is it? ")

    return name

def nameConfirmation():
    name = chooseName()
    answer = ""
    while answer != "yes" and answer != "no":
        answer = input("Right... So your name is " + str(name) + "? (yes or no) ")


    if answer == "yes":
        print("I have a grandson.")
        print("He's been your rival since you both were babies.")
        print("...Erm, what was his name now?")
        # raw_input for their name
        print("...Er, was it") #raw_input for name
        # Let user pick yes or no
        # If yes, move on
        # If no, ask name again
        print("That's right! I remember now! His name is") #raw_input for name
        print("your name!") # raw_input for name
        print("Your very own Pokémon legend is about to unfold!")
        print("A world of dreams and adventures with Pokémon awaits! Let's go!")
    if answer == "no":
        chooseName()
    return answer

displayIntro()
chooseGender()
nameConfirmation()

Upvotes: 0

Related Questions