[英]Python- Parts of my code aren't working and I can't figure out why
这是为了进入 python class。 我必须至少使用一个主要的 function 和 2 个其他功能。 当我运行时,它只是一遍又一遍地打印“你已经预订了旅行吗?请输入是或否”。 相反,我试图让它首先询问这个人是否已经预订了他们的旅行。 如果是,它会继续询问他们的性别。 然后是他们目的地的季节。 然后程序相应地生成一个装箱单。
# Welcome the user
def main():
print('Welcome the Personalized Packing List Generator. '
'In order to generate your list, you will have to provide some information about yourself and your trip.')
# function that checks whether the users' trip has been booked or not.
def tripBooked():
booked = input("Have you booked your trip already? Please enter yes or no.")
if booked == 'no' or booked == 'No':
print('Please return once your trip has been confirmed')
else:
print('Lets get to know you a bit better')
# Lists 1-4 are for everyone
list1 = ("Documents: Tickets, passport, itinerary.")
list2 = ("Clothing: Tops, pants, shoes, socks, undergarments.")
list3 = ("Electronics: Chargers, headphones.")
list4 = ('Hygiene: Toothbrush, toothpaste, floss, deodorant, hairbrush.')
# List 5 is for women
list5 = ("Makeup and cosmetics, jewelry, handbag, hair ties, straightener or curling rod.")
# List 6 is for men
list6 = ("Ties, cufflinks, razor, shaving gel, after shave.")
# List 7 for winter
list7 = ("Coat, hat, scarf, boots, warm socks, thermals.")
# List 8 is for fall/spring
list8 = ("Outerwear: Light jacket, cardigan, raincoat.")
# List 9 for summer
list9 = ('Flip flops, sunscreen, hat, bathing suit, sunglasses.')
finalList = list1 + list2 + list3 + list4
# List generator function
def genderListGenerator():
gender = input('What is your identified gender? Please enter male or female.')
print('Here is what we recommend as basic things to take with you:', finalList)
if "male" == genderListGenerator() or genderListGenerator() == 'Male':
print('finalList += list6')
elif genderListGenerator() == 'female' or genderListGenerator() == 'Female':
print('finalList += list5')
def seasonListGenerator():
season = input('What season is it where you are going? Please enter winter, fall, spring, or summer.')
if seasonListGenerator == "winter" or seasonListGenerator == 'Winter':
print('finalList += list7')
if "fall" or seasonListGenerator == 'Fall' or "spring" == seasonListGenerator or seasonListGenerator == 'Spring':
print('finalList += list8')
if seasonListGenerator == "summer" or seasonListGenerator == 'Summer':
print('finalList += list9')
def seasonListGenerator(): season = input('你要去的地方是什么季节?请输入冬天,秋天,spring,或夏天。') if season == "winter": print('finalList += list7') elif (season == "fall" or season == 'Fall' or season == "spring"): print('finalList += list8') else: print('finalList += list9') genderListGenerator() seasonListGenerator()
使用您的主 function 来控制程序的流程是个好主意。 此外,让您的函数返回值并将它们存储到主程序中的变量中,这样这些函数只被调用一次,并且用户的输入被保存以供进一步使用。 我冒昧地重写了您的代码,它现在应该可以更好地工作:
# Lists 1-4 are for everyone
list1 = ("Documents: Tickets, passport, itinerary.")
list2 = ("Clothing: Tops, pants, shoes, socks, undergarments.")
list3 = ("Electronics: Chargers, headphones.")
list4 = ('Hygiene: Toothbrush, toothpaste, floss, deodorant, hairbrush.')
# List 5 is for women
list5 = ("Makeup and cosmetics, jewelry, handbag, hair ties, straightener or curling rod.")
# List 6 is for men
list6 = ("Ties, cufflinks, razor, shaving gel, after shave.")
# List 7 for winter
list7 = ("Coat, hat, scarf, boots, warm socks, thermals.")
# List 8 is for fall/spring
list8 = ("Outerwear: Light jacket, cardigan, raincoat.")
# List 9 for summer
list9 = ('Flip flops, sunscreen, hat, bathing suit, sunglasses.')
# function that checks whether the users' trip has been booked or not.
def tripBooked():
booked = input("Have you booked your trip already? Please enter yes or no. ")
if booked == 'no' or booked == 'No':
print('Please return once your trip has been confirmed')
return False
else:
print('Lets get to know you a bit better')
return True
# List generator function
def genderListGenerator():
gender = input('What is your identified gender? Please enter male or female. ')
return gender
def seasonListGenerator():
season = input('What season is it where you are going? Please enter winter, fall, spring, or summer. ')
return season
# Welcome the user
def main():
print('Welcome the Personalized Packing List Generator. '
'In order to generate your list, you will have to provide some information about yourself and your trip. ')
if tripBooked():
finalList = [list1, list2, list3, list4]
gender = genderListGenerator()
if gender in ['male', 'Male']:
finalList.append(list6)
elif gender in ['female', 'Female']:
finalList.append(list5)
season = seasonListGenerator()
if season in ["winter", 'Winter']:
finalList.append(list7)
if season in ["fall", 'Fall', "spring", 'Spring']:
finalList.append(list8)
if season in ["summer", 'Summer']:
finalList.append(list9)
print('Here is what we recommend as basic things to take with you:',)
for l in finalList:
print(l)
if __name__ == "__main__":
main()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.