为了账号安全,请及时绑定邮箱和手机立即绑定

检查我的 Python 代码并建议我进行所需的编辑

检查我的 Python 代码并建议我进行所需的编辑

红糖糍粑 2024-01-27 16:06:12
我刚刚开始学习Python。我正在尝试学习制作一个程序,询问用户的姓名和他们想要访问的地点。并在调查结束时显示结果。但是,如果用户选择将调查传递给其他用户,则显示的投票结果仅包含回答这些问题的最后一个用户的姓名和地点。我该怎么做才能使程序显示所有参与投票的用户的结果。(代码是不言自明的)print("Hello world")qstn_1="What is your name?\t"qstn_2="Where would you like to GO for vacation?\t"polling_active=Truewhile polling_active: name=input(qstn_1) place=input(qstn_2) other=input("Would you like other person to answer these survey questions ?\t\n") polling_active=False if other=='yes':      polling_active=True else:    polling_active=False           print("\n\n\t\t\tPOLLING RESULTS\n")print("\n\n\t" + name.title() + " would like to go to " + place + " for a vacation!\n")
查看完整描述

2 回答

?
慕村9548890

TA贡献1884条经验 获得超4个赞

count = 0

names = []

places = []

while True:

    choice = input("Type N to take survey, type Y to pass it other user :")

    if choice == 'Y' and count==0:

        print("No one has taken this survey")

        break



    elif choice == 'N':

        name = input("What is your name :")

        place = input("Where would you like to visit :")

        names.append(name)

        places.append(place)

        count += 1    


    elif choice == 'Y':

        print("The last user was", names[-1], "and", names[-1], "wanted to visit", places[-1])

        break


print("\n\n\t\t POLLING RESULTS\n")

for name, place in zip(names, places):

    print(name, "wanted to visit", place,"\n")


查看完整回答
反对 回复 2024-01-27
?
慕哥6287543

TA贡献1831条经验 获得超10个赞

一些修复:


添加了列表,最后需要解压列表。

print("Hello world")

qstn_1="What is your name?\t"

qstn_2="Where would you like to GO for vacation?\t"

polling_active=True

name_lst = []

place_lst = []

while polling_active:

    name_lst.append(input(qstn_1))

    place_lst.append(input(qstn_2))

    other=input("Would you like other person to answer these survey questions ?\t\n")

    polling_active=False

    if other=='yes':

        polling_active=True


      

    

print("\n\n\t\t\tPOLLING RESULTS\n")

for i_name, i_place in zip(name_lst, place_lst):

    print("\n\n\t" + i_name.title() + " would like to go to " + i_place + " for a vacation!\n")



查看完整回答
反对 回复 2024-01-27
  • 2 回答
  • 0 关注
  • 104 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信