Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数循环是真还是假_Python_While Loop_Return_Function - Fatal编程技术网

Python 函数循环是真还是假

Python 函数循环是真还是假,python,while-loop,return,function,Python,While Loop,Return,Function,我现在正在学习python中的函数。我遇到一个练习,要求我编写一个脚本,告诉用户该学生是否在课堂上。以下是脚本应显示的结果: Welcome to the student checker! Please give me the name of a student (enter 'q' to quit): [student1] No, that student is not in the class. Please give me the name of a student (enter 'q'

我现在正在学习python中的函数。我遇到一个练习,要求我编写一个脚本,告诉用户该学生是否在课堂上。以下是脚本应显示的结果:

Welcome to the student checker!
Please give me the name of a student (enter 'q' to quit): [student1]
No, that student is not in the class.
Please give me the name of a student (enter 'q' to quit): [student2]
Yes, that student is enrolled in the class!
Please give me the name of a student (enter 'q' to quit): q
Goodbye!
我已经写了一个符合要求的脚本。以下是脚本:

print "Welcome to the student checker!"

students = ['Andreas', 'Martina', 'Maria']

while True:
    name_student = raw_input("Please give me the name of a student (enter 'q' to quit): ")

    if name_student == 'q':
        print "Goodbye!"
        break

    if name_student in students:
        print "Yes, {} is enrolled in the class!".format(name_student)

    elif name_student not in students:
        print "No, {} is not in the class.".format(name_student)
但是这个练习说明,如果学生在场,应该有一个函数返回True,如果学生不在场,则返回False

有人能告诉我如何修改脚本以满足添加一个函数的要求吗?如果学生在场,该函数将返回True,如果学生不在场,则返回False


提前谢谢!!和平

试试这样做:

print "Welcome to the student checker!"

students = ['Andreas', 'Martina', 'Maria']

def is_present(name_student):
    if name_student in students:
        print "Yes, {} is enrolled in the class!".format(name_student)
        return True

    elif name_student not in students:
        print "No, {} is not in the class.".format(name_student)
        return False

while True:
    name_student = raw_input("Please give me the name of a student (enter 'q' to quit): ")

    if name_student == 'q':
        print "Goodbye!"
        break

    is_present(name_student)
将逻辑封装到函数中,这样它就可以返回一个值,并在每次无限循环迭代时调用它

@编辑
@barak manos是对的,我更新了代码,以下是将学生测试转换为函数的一种方法:

def student_present(student):
    return student in ['Andreas', 'Martina', 'Maria']

print "Welcome to the student checker!"

while True:
    name_student = raw_input("Please give me the name of a student (enter 'q' to quit): ")

    if name_student == 'q':
        print "Goodbye!"
        break

    if student_present(name_student):
        print "Yes, {} is enrolled in the class!".format(name_student)
    else:
        print "No, {} is not in the class.".format(name_student)
注[xxxxxx]中的
student会自动为您提供一个可直接返回的布尔结果。无需显式返回
True
False

另一点要考虑的是,如果输入了<玛蒂娜> <代码>,会发生什么?使用当前代码,它将声明

不在类中
。如果您按以下方式更改函数,它也会接受:

def student_present(student):
    return student.lower() in ['andreas', 'martina', 'maria']

我会把
raw_input
行放在函数外部,然后将输入传递给函数。啊哈..现在我知道应该在哪里以及如何使用函数了。谢谢大家!!!你们都是最棒的!!!谢谢马丁!!!你的解释很清楚,可以理解!!!为此干杯!!在我的编程过程中,我需要你和这个社区的专家给我很多建议!!谢谢大家:)