简体   繁体   English

使用输入参数进行Python循环

[英]Python Looping with input parameters

I am new to programming and I am trying to write a python script where the user is asked if they would like to add a name and student to the student list. 我是编程新手,我试图编写一个python脚本,询问用户是否要将名称和学生添加到学生列表中。 If they say yes, then they are asked for the input of the name and the id. 如果他们回答“是”,则要求他们输入名称和ID。 However, if they say no, its suppose to print whatever is already stored in the student list. 但是,如果他们拒绝,则它会打印出学生列表中已存储的所有内容。

This is my code: 这是我的代码:

students = [ ]

def prompt_add():

add_more_student = bool(input("Do you want to add students to this list? (yes/NO):"))

add_student(())

if bool == 'yes':
        add_student(())
else:

    print(students)

def add_student(name, student_id=133):

    student_name = input("Enter a name:")
    student_id = input("Enter number:")

    student = {"name": name, "student_id": student_id}
    students.append(student)
    prompt_add()
    return students

prompt_add()

This is the output it keeps printing: 这是它一直打印的输出:

屏幕截图

Turns out that regardless of if I type yes or no, I still get the question asked. 事实证明,无论我输入是还是否,我仍然会问这个问题。 Please, what am I doing wrong? 拜托,我在做什么错? thank you in advance. 先感谢您。

i dont know if this is what u wanted but here it is: "while True" just let it loops over and over again. 我不知道这是否是您想要的,但在这里是:“ while True”只是让它一遍又一遍地循环。

students = [ ]
def add_student(name, student_id=133):
   student_name = input("Enter a name:")
   student_id = input("Enter number:")

   student = {"name": student_name, "student_id": student_id}
   students.append(student)
   return students


while True:
   add_more_student = input("Do you want to add students to this list? (yes/NO):")
   if add_more_student == 'yes':
           add_student(())
           print(students)
   elif add_more_student == "no":
       print(students)
       break

hopefully this is what u were looking for ^^ edit: sorry it shoudnt be: 希望这是您正在寻找的^^编辑:对不起,应该是:

elif add_more_student == "no":

but

else:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM