Python代码:函数调用不起作用或cod有问题

2024-09-29 19:35:16 发布

您现在位置:Python中文网/ 问答频道 /正文

PYTHON版本:2.7.5

我正在做一个小程序,它根据用户的输入为用户生成一个自定义句子。我试着使name()函数位于chooseColour()函数之前。我以前在代码中调用过name()函数,但它没有出现。我得到的只是'你最喜欢的颜色是什么',而不是首先显示name()函数。我将附上代码,如果有人可以复制和更正它,我可以只使用您编辑的代码。你知道吗

# DoC: 07/12/2016
# Creator: Louis Parkes
# Desc: This program will generate a custom sentence depending on the user's imput.

import time

def displayIntro():
    print("Welcome to the CSG (Custom Sentence Generator)")
    print("Please answer the following questions to create the sentence.")
    print("")
    print("Creator: Louis Parkes")
    print("Contact: my-email")
    print("")
    s1()

def displayEnd():
    print("Thanks for using the CSG!")
    print("We hoped you enjoy the experience!")
    rateByUser = int(raw_input("Please rate your experience, 1 being 1 star and 5 being 5 star."))
    if rateByUser == 1 or rateByUser == 2 or rateByUser == 3 or rateByUser == 4:
        print("Please help us improve by emailing 'my-email'")
    elif rateByUser == 5:
        print("Thanks very much for your response! Have a great day!")
    else:
        print("Error: invalid input.")

def s1():
    time.sleep(1)
def s2():
    time.sleep(0.5)

#==================================================================================================

def name():
    forename = ""
    surname = ""
    while forename == 0 and surname == 0:
        forename = raw_input("What is your forename?: ")
        print("")
        time.sleep(1)
        surname = raw_input("What is your surname?: ")
        print("")
        time.sleep(1)
        print("Your name is %s %s! " % (forename, surname))
        time.sleep(0.5)

    return forename
    return surname

    if len(forename) > 5 or len(surname) > 6:
        print("You have a large name, %s! " % (forename, surname))
    elif len(forename) <= 5 or len(surname) <= 6:
        print("You have an average sized name!")
    elif len(forename) > 5 or len(surname) > 6:
        print("You have a small name!")
    else:
        print("Error: Obviously, there is something not right with your input or the code. Please try again. Contact developer if there is a bug.")

s1()
def chooseColour():
    colour = ""
    while len(colour) == 0:
        colour = raw_input("What is your favourite colour?: ")
        s2()
        print("Your favourite colour is %s! " % (colour))

    return colour

displayIntro()
name()
chooseColour()

Tags: orthenameinputyourlentimeis
3条回答
def name():
    forename = ""
    surname = ""
    while not forename and not surname:
        forename = raw_input("What is your forename?: ")
        print("")
        time.sleep(1)
        surname = raw_input("What is your surname?: ")
        print("")
        time.sleep(1)
        print("Your name is %s %s! " %(forename, surname))
        time.sleep(0.5)


    if len(forename) > 5 or len(surname) > 6:
        print("You have a large name, %s%s! " % (forename, surname))
    elif len(forename) <= 5 or len(surname) <= 6:
        print("You have an average sized name!")
    elif len(forename) > 5 or len(surname) > 6:
        print("You have a small name!")
    else:
        print("Error: Obviously, there is something not right with your input or the code. Please try again. Contact developer if there is a bug.")

    return forename
    return surname
In [1]: forename = ""

In [2]: forename == 0
Out[2]: False

在name()中,当名字和姓氏为空(不等于0)时,应在中进行测试。你知道吗

在name函数中有2个返回。只有第一个会被执行,之后什么也不会执行。你知道吗

您的name()调用没有效果,因为:

forename = ""
surname = ""
while forename == 0 and surname == 0:

循环条件永远不会满足,因为"" != 0,所以循环块不会执行。更改为:

while forename == "" and surname == "":

或:

while not (forename or surname):

相关问题 更多 >

    热门问题