簡體   English   中英

我是編碼新手,正在嘗試學習 python,但我在使用 if、elif 和 else 時遇到了麻煩

[英]I'm new to coding and trying to learn python and I'm having trouble with, if, elif, else

我是編碼新手,正在嘗試自學 Python。 我啟動了我的第一個程序並使用了ifelifelse語句,但我一直收到語法錯誤。 我附上了一個屏幕截圖,以嘗試更容易地解決這個問題。

令人討厭的代碼截圖

如果我們想編寫 if,elif,else 條件,我們應該將所有內容都寫在 if,elif,else 條件中。

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

您的代碼中的問題是您在 if 循環之外編寫了 wave_obj 文件。它表明在來自 if 條件之后它將轉到該對象(wave_obj)。 我們應該連續寫 if,elif,else 條件。 如果您想提及對象(wave_obj),您可以在 if,else,elif 條件之后提及對象。

並在 if 和 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()

您的代碼存在一個主要問題。 Python 使用縮進。 因此,在 if、elif 和 else 語句的前兩行之后,其余部分運行一次,而不是由 if、elif 或 else 語句觸發。 現在,在您取消縮進后,python 將不接受 elif 語句(示例如下):

a = 4
if(a<5):
    print("a is less than 5")
print(a)
elif(a == 5):
    print("a is equal to 5")

正如你在上面看到的,我們的 print(a) 沒有縮進。 這會產生一個錯誤,因為 python 會認為在 elif 語句之前沒有 if。 額外說明:導入應該只在代碼的頂部。 一旦你在那里導入了一些東西,就不需要在你的代碼中間再次導入它。

正確(和優化)代碼:

import simpleaudio as sa
wave_obj = sa.WaveObject.from_wave_file("home/kevin/Downloads/audio-8")
play_obj = wave_obj.play()
play_obj.wait_done()
answer = input("Please type Start or Quit Here")
if answer == 'start' or 'Start': #== is case sensitive
    print("The Warp Core Interface is Initializing")
    wave_obj = sa.WaveObject.from_wave_file("home/kevin/Downloads/audio-5")
    play_obj = wave_obj.play()
    play_obj.wait_done()
elif answer == 'quit' or 'Quit':
    print(" The self destruct sequence has begun")
    wave_obj = sa.WaveObject.from_wave_file("home/kevin/Downloads/audio-6")
    play_obj = wave_obj.play()
    play_obj.wait_done()
else:
    print("You cannot type start or quit?")
    wave_obj = sa.WaveObject.from_wave_file("home/kevin/Downloads/audio-7")
    play_obj = wave_obj.play()
    play_obj.wait_done()

更正了一些縮進,無需多次導入 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM