i making quiz in swift playgrounds app , want keep counter of number of correct answers. value of correct
doesn't change, , don't know why.
var correct:int = 0 func qa(question: string, answer: string) { show(question) var ans = ask("answer") if (ans.lowercased() == answer) { show("correct") correct = correct + 1 } else { show("wrong numpty!") } } func qaor(question: string, answer: string, answer2: string) { show(question) var ans = ask("answer") if (ans.lowercased() == answer) || (ans.lowercased() == answer2) { show("correct") correct = correct + 1 } else { show("wrong numpty!") } } show("what name?") let name = ask("name") show("hi " + name) qa(question: "what name of character played simon jones in hitchikers guide galaxy?", answer: "arthur dent") qaor(question: "what voiced peter jones in hitchikers guide galaxy?", answer: "the book", answer2: "the guide") qa(question: "finish sentence .doing coastlines favourite, rough crinkley edges, .... ", answer: "fjords") var cf = "no" if (correct == 0) { var cf = "no" } else if (correct == 1) { var cf = "one" } else if (correct == 2) { var cf = "two" } else if (correct == 3) { var cf = "three" } show("you got " + cf + " questions correct out of three")
do not declare cf
variable every if/else statement. creates local variable , aren't changing first cf
.
instead, try this:
var cf = "no" if (correct == 0) { cf = "no" } else if (correct == 1) { cf = "one" } else if (correct == 2) { cf = "two" } else if (correct == 3) { cf = "three" } show("you got " + cf + " questions correct out of three")
Comments
Post a Comment