【问题标题】:While loop only running once even though it worked fine earlierWhile 循环只运行一次,即使它之前运行良好
【发布时间】:2023-03-09 13:18:01
【问题描述】:

我有一个我调用的函数,因此我可以将名称添加到列表中。除非我输入“结束”或“结束”,否则我想继续将名称添加到列表中。我正在使用设置为 True 的 While 循环来执行此操作,并使用 if/else 语句来附加列表。但是,当我运行我的代码时,它只在离开之前运行一次 While 循环。我有一个类似的函数做同样的事情,除了用数字而不是名字,这没问题。 我之前使用过它,它没有任何问题,但我看不出发生了什么改变以使 While 循环不起作用。

Student_Names=[]                                                                                                                                                                         
def names():                                                                                          
    student_name=str(input("Please enter the students name and type when complete: "))                
    Student_Names.append(student_name)
    while True:                                                                                       
        student_name=str(input("Please enter the students name and type when complete: "))            
        if student_name ==('end') or ('End'):
            break
        else:
            Student_Names.append(student_name) 
names()

非常感谢任何帮助

【问题讨论】:

    标签: python loops while-loop


    【解决方案1】:
    Student_Names=[]                                                                                                                                                                         
    def names():                                                              
        student_name=str(input("Please   
        enter the students name and type when complete: "))                
        Student_Names.append(student_name)
        while True:                                                                                       
            student_name=str(input("Please     
            enter the students name and 
            type when complete: "))            
            if student_name =="end" or student_name=="End":
                break
            else:
                Student_Names.append(student_name) 
    names()
    

    更改它的 if 语句。

    【讨论】:

    • Live saver,非常感谢。度过一个愉快的夜晚
    猜你喜欢
    • 1970-01-01
    • 2011-11-02
    • 2020-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-17
    • 2014-12-18
    • 2021-04-04
    相关资源
    最近更新 更多