python3中else语句的语法无效

时间:2018-09-02 01:26:06

标签: python python-3.x syntax

当我尝试运行我的代码时,它说我的else语句语法无效,但是我无法弄清楚出了什么问题。

import random
import time
username = input("Hello.  Please enter your name, then press 'enter'.  
After you type something, you will need to /n"
                 "click the 'enter' key to send it")
print ("Hello " + username)
time.sleep(3)
game_tutorial_input = input("Do you wish to see the tutorial? (y/n)")
if game_tutorial_input == "y":
    print ("Great!  Press enter after each instruction to move /n" 
"onto the next one.")
    else
        print("Are you sure? (y/n")

4 个答案:

答案 0 :(得分:3)

在python中缩进很重要,请确保elseif位于同一缩进列。如前所述,您需要在:的末尾加上else。即else:

答案 1 :(得分:0)

尝试“其他:”

我相信它缺少冒号。

答案 2 :(得分:0)

  1. 缩进else

  2. else

  3. 的末尾添加一个冒号

说明:

  1. elseifelifwith等是在语句后需要冒号的语句

答案 3 :(得分:0)

在其他字符后加冒号,而其他字符的缩进是错误的

class PageThreeActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_page_three)

    val btnListen = findViewById<Button>(R.id.btnListen)
    btnListen.setOnClickListener() {
        Toast.makeText(this@PageThreeActivity, "This is a Toast Message", Toast.LENGTH_SHORT).show()
    }
}// end onCreate

fun onLISTEN(view: View) {

    val intent = Intent(this@PageThreeActivity, MainActivity::class.java)
    startActivity(intent)
}