Python-我的部分代码无法正常工作,我不知道为什么

时间:2019-10-07 20:19:29

标签: python function functional-programming main

这是一个进入python的类。我必须至少使用一个主要功能和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')

2 个答案:

答案 0 :(得分:0)

def seasonListGenerator():     季节=输入(“您打算去哪个季节?请输入冬天,秋天,     春天还是夏天。”)     如果季节==“冬天”:         print('finalList + = list7')     elif(季节==“秋天”或季节=='秋天'或​​季节==“春季”):         print('finalList + = list8')     其他:         print('finalList + = list9')     sexListGenerator()     seasonListGenerator()

答案 1 :(得分:0)

最好使用您的主要功能来控制程序的流程。同样,使函数返回值并将它们存储到主程序中的变量中,这样,这些函数仅被调用一次,并且用户输入被保存以备将来使用。 我随意重写了代码,现在应该可以更好地工作了:

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