如何修复Python代码中Elif语句内添加input产生的语法错误
问题原因
你代码的语法错误出现在y == "engineering"的判断分支中:
你错误使用了双等号==做变量赋值操作,该行代码z == input("Enter Stream: ")里的==是比较运算符,仅用于判断两个值是否相等,给变量z赋值需要使用单等号=。
解决方案
仅修改这一行的运算符即可,原有其他逻辑完全不需要改动:
x = input("Enter your name: ") y = input("Enter your field: ") print("Hello %s,You have a very interesting Field" % x) print("Here are the future options for all the careers related to %s:"% y) if y == "commerce": print("A.Chartered accountant\nB.Civil Services\nC.Accountant\nD.company Secretary\nE.Financial Analyst\nF.Auditor") elif y == "biology": print("A.Doctor\nB.pharmacist\nC.Chemist\nD.Scientist") elif y == "engineering": # 把原来的比较运算符==改成赋值运算符=即可 z = input("Enter Stream: ") if z == "Mechanical": print("Mechanical Engineer") elif z == "computer science": print("A.Software Engineer\nB.Data Scientist\nC.Programmer") else: print("I suppose you've entered Arts,YOu can opt for a career in\nA.IAS\nB.Music\nC.Political Science")
内容的提问来源于stack exchange,提问作者Aditya Singh
相关产品推荐
相关产品推荐

