在python中是或否输出

时间:2016-11-24 18:36:51

标签: python-3.x if-statement

我是python的新手我试图编写这个,我问一个问题,因此"你是一个突变体"并且根据用户是否响应,它应该出现在相应的输出中,但它仅适用于是,但不适用于否。我如何让它适用于elif输出?

import UIKit
import SpriteKit
import GameplayKit

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if let view = self.view as! SKView? {
            // Load the SKScene from 'GameScene.sks'
if let scene = GameScene.level(levelNum: 18)  {
    // Set the scale mode to scale to fit the window
                scene.scaleMode = .aspectFill

                // Present the sc ene
                view.presentScene(scene)
               // view.showsPhysics = true
            }

            view.ignoresSiblingOrder = false

            view.showsFPS = true
            view.showsNodeCount = true
        }
    }

`

2 个答案:

答案 0 :(得分:1)

您需要将存储用户输入的变量与您要比较的事物进行比较。在这种情况下使用==。以下是您的示例修改后的代码:

print("Are you a mutant?")
answer = input()
if answer == 'Yes':
    print("Your application to Xavier's School for Gifted Youngsters has been accepted")
elif answer == 'No':
    print("Your application was not successful on this occassion")

答案 1 :(得分:0)

您必须编写raw_input而不是输入。 “输入”仅采用文本值,而“ raw_input”将输入作为字符串。 如果您使用的是python2,请遵循以下代码:

print("Are you a mutant?")
answer = raw_input("Yes/No: ")

if answer == "Yes":
    print("Your application to Xavier's School for Gifted Youngsters has been accepted")
elif answer == "No":
    print("Your application was not successful on this occasion")

在python3中,raw_input()重命名为input()。然后按照以下代码进行操作:

print("Are you a mutant?")
answer = input("Yes/No: ")

if answer == "Yes":
    print("Your application to Xavier's School for Gifted Youngsters has been accepted")
elif answer == "No":
    print("Your application was not successful on this occasion")