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()
在name()中,当名字和姓氏为空(不等于0)时,应在中进行测试。你知道吗
在name函数中有2个返回。只有第一个会被执行,之后什么也不会执行。你知道吗
您的
name()
调用没有效果,因为:循环条件永远不会满足,因为
"" != 0
,所以循环块不会执行。更改为:或:
相关问题 更多 >
编程相关推荐