Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 3.x 为什么不执行if语句?_Python 3.x_If Statement - Fatal编程技术网

Python 3.x 为什么不执行if语句?

Python 3.x 为什么不执行if语句?,python-3.x,if-statement,Python 3.x,If Statement,我有一个作业,在作业中我必须询问用户是否希望向程序中添加新学生。 我设置了一个变量来保存用户命令,然后: 如果是(1),程序将要求用户输入名称和id,如果不是,程序将退出 students = [] def get_students_titleCase(): students_titleCase = [] for student in students: students_titleCase = student["name"].title() retur

我有一个作业,在作业中我必须询问用户是否希望向程序中添加新学生。 我设置了一个变量来保存用户命令,然后: 如果是(1),程序将要求用户输入名称和id,如果不是,程序将退出

students = []

def get_students_titleCase():
    students_titleCase = []
    for student in students:
        students_titleCase = student["name"].title()
    return students_titleCase

def print_students_titlecase():
    students_titleCase = get_students_titleCase()
    print(students_titleCase)

def add_student(name , student_id = 000):
    student = {"name" : name , "student_id": student_id }
    students.append(student)

user_command = input("Do You Want to Add a Student Name?\n1- Yes\n2- No\n")
#user_command = 1
if user_command == 1:
    student_list = get_students_titleCase()
    student_name = input("Enter Student Name: ")
    student_id = input("Enter Student ID: ")
    add_student(student_name, student_id)
    print_students_titlecase()

如果在
python2.7
上执行,行为将与预期一样,但因为其
python3
where
input
返回
str
类型,如
raw\u input
python2.7
中返回。还请注意,
raw\u输入
不是在
python3
中引入的

因此,考虑到以上这一点,您必须稍微更改代码以使其工作

# Compare against string instead of int
if user_command == '1':
但是,如果您仍要与
int
类型的数量进行比较,则

if user_command.isdigit() and int(user_command) == 1:

因为
user\u命令
不等于
1
。。。(提示:
input
返回什么?)???(这里还有另一个提示:阅读文档…)idk你的意思是什么,当我运行程序时,出现用户_命令行“Do u want add…”文本,我按1
如果提示参数存在,它将写入标准输出,而不带尾随换行符。然后,函数从输入中读取一行,将其转换为字符串(去掉尾随的换行符),并返回该字符串。