为什么我的函数执行两次?

时间:2017-12-07 04:09:00

标签: python

我的代码中的函数重复两次。在chooseName()函数中,它会询问问题,一旦您回答,它会重复该问题,然后继续执行其余代码。为什么这样做?

# -*- 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()

我很抱歉没有及早发布其余的代码。 这是输出。

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

4 个答案:

答案 0 :(得分:0)

chooseName()首先在chooseGender()的调用下调用两次,在nameConfirmation()调用后调用第二次,这样就会导致它被执行两次。如果已修复,您可以评论或删除chooseName() chooseGender(),如下所示。

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

如此更新的代码如下:

# -*- 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()

答案 1 :(得分:0)

代码中的一些修改,这是更新的代码:

  

首先编辑:

您正在调用chooseName()两次,

  

第二次编辑:

您在程序的主要逻辑之前返回。

这里

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":yes,您的no代码也不会执行

因此,请在程序的最后一个位置返回,或者如果您想在同一个地方返回,请在那里使用print而不是' 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()

答案 2 :(得分:0)

您调用函数choseName(),然后通过调用nameConfiguration()致电choseName()后立即调用,这就是为什么它被调用两次。您只需删除choseName()之前的nameConfiguration()

答案 3 :(得分:0)

删除chooseName( )以下的chooseGender( )调用,因为它已在nameConfirmation( )定义中调用。它对我有用。

相关问题