PYTHON版本:2.7.5
我正在制作一个小程序,根据用户的输入为用户生成自定义句子。我试图使它成为name()函数在chooseColour()函数之前。我之前在代码中调用了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()
答案 0 :(得分:1)
In [1]: forename = ""
In [2]: forename == 0
Out[2]: False
在名字()中,你应该测试forename和surname是否为空,不等于0。
您在名称功能中有2个返回。只会执行第一个,之后不执行任何操作。
答案 1 :(得分:0)
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
答案 2 :(得分:0)
您的name()
电话无效,因为:
forename = ""
surname = ""
while forename == 0 and surname == 0:
因为"" != 0
永远不满足循环条件,所以不执行循环块。将其更改为:
while forename == "" and surname == "":
或:
while not (forename or surname):
答案 3 :(得分:0)
你的问题是在while循环中:
def name():
forename = ""
surname = ""
while forename == "" and surname == "":
您需要将这两个变量与""
(空字符串)进行比较,而不是0
(整数)