How do I print the boolean value?

DiscountRonWeasley

Okay, so I'm currently trying to write the code for a very inaccurate stoplight--one that is only meant to help me understand what I've learned, but I can't seem to figure it out! In my code, at the very end, I say print("Green Light!") after setting up a few variables, but I would like to indicate what the computer should do instead of flat out saying "print this phrase," if that makes sense... not sure it does.

How would I write this if I wanted to simply print the Boolean value of greenLight without saying print("Green Light!")?

I'm very much a beginner and I might be asking the wrong question--I know that, but I'm hoping someone can help!

Something tells me I haven't learned enough to do this, yet, but I really wanna know how this works.

This is what I've written so far. It runs, but I would like to change it so all I have to say is print(greenLight) or print(Bool).

When I try putting in print(greenLight), it returns an error:

Output: Review.swift:14:7: error: variable 'greenLight' used before being initialized print(greenLight) ^ Review.swift:4:5: note: variable defined here var greenLight: Bool ^

var carAtRightIntersection = false
var carAtLeftIntersection = false
var carStraightAhead = true
var greenLight: Bool

if !(carAtRightIntersection && carAtLeftIntersection) && carStraightAhead {
  greenLight = true
}

if carAtRightIntersection && carAtLeftIntersection && !(carStraightAhead) {
  greenLight = false
}

print("Green light!")
hermanj96

If you don't want to print the actual phrases, You could use a switch case statement

switch greenLight{

case true:
    print("Green Light!")

case false:
    print("Red Light!")

default:
    print("Yellow Light!")
}

After the first block executes

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I print out the value of this boolean? (Java)

How to print a boolean value?

How do I use if/else shortcut to print string based on boolean value

How do I print out the dictionaries within a list that has a boolean value in Python?

Given a list of elements that evaluate to boolean values, how do I print the element name and not the value?

How do I assign a boolean object to a value?

How do I check a value in a boolean List?

How do I use a boolean Value as Trigger

how can i print a boolean value in JSON in table

How to print boolean return value

How do I print the first and last value?

How do I print return value of a function?

How do I print the highest value in a class?

How do I print the return value on the console?

how do i print a value of a document in pymongo

How do I get the value of a boolean from a database field?

How do I pass Nullable<Boolean> value to CommandParameter?

How do I assign a value in numpy with advanced boolean indexing?

How do I check the Boolean value on eval() or if an arithmetic operation is false?

How do I get a boolean value from an input?

How do I insert a Boolean Value into my SQLite Database?

How do I change the value of a boolean method in a method?

I am trying to print a boolean value in HLA program, but it shows a error . I dont understand where did I do wrong?

awk: how do I print the value of a key in an env file?

How do I print value from hashmap json object

How do I print the full value of a long string in gdb?

How do I print Fetch promise value in ReactJs?

Java - How do I print the equation with the value of the variables?

How do I get and print value of an environment variable?