我是编码和尝试学习python的新手,我遇到了麻烦,如果,elif,别的

时间:2017-01-04 19:38:39

标签: python

我是编码的新手,我正在尝试教我的自我蟒蛇。我启动了我的第一个程序并使用了if,elif,else语句,但我一直收到无效的语法错误。我要包含一个screen shot来尝试让它更容易理解。

2 个答案:

答案 0 :(得分:0)

如果我们想写if if,elif,else条件我们应该写if if,elif,else条件中的所有内容。

if(condition):
    #your code
elif(condition):
    #your code
else:
    #your code

你的代码中的问题是你在if循环之外写了wave_obj文件。它表示在来自if条件之后它会转到那个对象(wave_obj)。我们应该写ifif,elif,else条件连续。如果你想提及对象(wave_obj),你可以在if,else,elif条件之后提及对象。

并在elif

之前和之后编写代码
if answer == 'start':
    print 'the wrap core interface'
    wave_obi = sa.WaveObject.from_wave_file('/home/kevin/Downloads/audio-5')
    play_obj = wave_obj.play()
    play_obj.wait_done()

elif answer == 'quit':
    print 'the self destruct has begin'
    sa.WaveObject.from_wave_file('/home/kevin/Downloads/audio-6')
    play_obj = wave_obj.play()
    play_obj.wait_done()

else:
    print 'really numb nuts....'
    sa.WaveObject.from_wave_file('/home/kevin/Downloads/audio-7')
    play_obj = wave_obj.play()
    play_obj.wait_done()

答案 1 :(得分:-1)

更正了一些缩进,无需多次导入simpleaudio。

#!/usr/bin/env python3

import simpleaudio as sa
# Your code here
# Your code here
# Your code here
answer = input("Please type Start or Quit here numb nuts: ")
if answer == 'start':
    print("The Warp Core Interface is Initializing")
    # No need to import simpleaudio again
    # Your code here
    # Your code here
    # Your code here
elif answer == 'quit':
    print("The self distruct sequence has begun")
    # No need to import simpleaudio again
    # Your code here
    # Your code here
    # Your code here
else:
    print("Really numb nuts, You cannot type start or quit?")
    # No need to import simpleaudio again
    # Your code here
    # Your code here
    # Your code here