What is the correct syntax for #39;else if#39;?(else if 的正确语法是什么?)
问题描述
我是一名新的 Python 程序员,正在从 2.6.4 飞跃到 3.1.1.在我尝试使用else if"语句之前,一切都很好.解释器在else if"中的if"之后给了我一个语法错误,原因我似乎无法弄清楚.
I'm a new Python programmer who is making the leap from 2.6.4 to 3.1.1. Everything has gone fine until I tried to use the 'else if' statement. The interpreter gives me a syntax error after the 'if' in 'else if' for a reason I can't seem to figure out.
def function(a):
if a == '1':
print ('1a')
else if a == '2'
print ('2a')
else print ('3a')
function(input('input:'))
我可能遗漏了一些非常简单的东西;但是,我自己无法找到答案.
I'm probably missing something very simple; however, I haven't been able to find the answer on my own.
推荐答案
在python中else if"拼写为elif".
此外,您需要在 elif 和 else 之后使用冒号.
In python "else if" is spelled "elif".
Also, you need a colon after the elif and the else.
简单问题的简单答案.当我第一次开始时(在过去几周内),我遇到了同样的问题.
Simple answer to a simple question. I had the same problem, when I first started (in the last couple of weeks).
所以你的代码应该是:
def function(a):
if a == '1':
print('1a')
elif a == '2':
print('2a')
else:
print('3a')
function(input('input:'))
这篇关于'else if' 的正确语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:'else if' 的正确语法是什么?
基础教程推荐
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
