vendredi 1 janvier 2016

Unable to enter if statement after raw_input

Whenever I call a function within a while loop in my project it will do absolute nothing according to the function just being called and will just continue to refresh the loop like nothing happened.

Here is a simple example I wrote demonstrating the problem. By running the code you will be shown a menu and you will need to enter a choice from the menu. When doing so a few "if" statements will check which option you chose and call and execute the code below them if the choice doesn't belong to any of the statements the menu will just refresh:

#!/usr/bin/env python

import os
import time

def test():
    x = True
    while True:
        if not x:
            print "You need to type 1\n"
        choice = raw_input("type 1 here: ")
        if choice == 1:
            print 'Works!\n'
            time.sleep(5)
            break
        else:
            x = False

def test2():
    print "Test123\n"

try:
    while True:
        os.system("clear")

        menu_choice = raw_input("Enter Choice: ")

        if menu_choice == 1:
            test()

        if menu_choice == 2:
            test2()

        if menu_choice == 3:
            os.system("python")

except:
    pass

If else validation, moving on by clicking on a button with a specific condition

Ce résumé n'est pas disponible. Veuillez cliquer ici pour afficher l'article.

Swift email add check

I am quite new to SWIFT. I am trying the following checking method on my demo app. What is that I am doing wrong? Because the else if is never kicking in

class SignupClass: UIViewController {

    @IBOutlet var nameActualUser: UITextField!
    @IBOutlet var prefUsername: UITextField!
    @IBOutlet var emailSignup: UITextField!
    @IBOutlet var passSignup: UITextField!
    @IBOutlet var phoneSignup: UITextField!

    @IBAction func lawyerSignup(sender: AnyObject) {


       hideRegistrationText()
    }

    @IBOutlet var regNumberSignup: UITextField!


    @IBAction func validateEmail(sender: AnyObject) {

        if emailSignup.text!.isEmpty {

            let myAlert = UIAlertController(title: "Notification", message: "Enter email address", preferredStyle: UIAlertControllerStyle.Alert);

            let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default){(ACTION) in
                print("Ok Button Tapped");
            }

            myAlert.addAction(okAction)

            self.presentViewController(myAlert, animated: true, completion: nil);
            print("enter email address") //prompt ALert or toast
        }
        else if validate(emailSignup.text!)
        {

            let myAlert = UIAlertController(title: "Notification", message: "Invalid email addresses", preferredStyle: UIAlertControllerStyle.Alert);

            let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default){(ACTION) in
                print("Ok Button Tapped");
            }

            myAlert.addAction(okAction)

            self.presentViewController(myAlert, animated: true, completion: nil);
            print("Invalid email address") // prompt alert for invalid email
        }


    }

    @IBOutlet var lawyerSwitch: UISwitch!

    @IBAction func signupButton(sender: AnyObject) {
        validateEmail(emailSignup.text!)

        signupMethod()

    }





    func validate(emailAdd: String) -> Bool
    {
        let REGEX: String
        REGEX = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,6}"
        return NSPredicate(format: "SELF MATCHES %@", REGEX).evaluateWithObject(emailAdd)
    }


    func hideRegistrationText()
    {
        if lawyerSwitch.on
        {
             regNumberSignup.hidden = false
        }
        else
        {
            regNumberSignup.hidden = true
        }

    }

    func signupMethod()
    {
        let user = PFUser()
        user.username = prefUsername.text
        user.password = passSignup.text
        user.email = emailSignup.text

        user["name"] = nameActualUser.text
        user["phone"] = phoneSignup.text

        user.signUpInBackgroundWithBlock{
            (succeeded: Bool, error:NSError?) -> Void in
            if let _ = error{
                let myAlert = UIAlertController(title: "Notification", message: error!.localizedDescription, preferredStyle: UIAlertControllerStyle.Alert);

                let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default){(ACTION) in
                    print("Ok Button Tapped");
                }

                myAlert.addAction(okAction)

                self.presentViewController(myAlert, animated: true, completion: nil);            }
            else
            {
                let myAlert = UIAlertController(title: "Notification", message: "Succesfully Signed Up!", preferredStyle: UIAlertControllerStyle.Alert);

                let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default){(ACTION) in
                    print("Ok Button Tapped");
                }

                myAlert.addAction(okAction)

                self.presentViewController(myAlert, animated: true, completion: nil);

            }

        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        regNumberSignup.hidden = true

        nameActualUser.text = "werewdfsd"
        prefUsername.text = "edfvccer"
        //emailSignup.text = ""
        passSignup.text = "234324"
        phoneSignup.text = "1231243"



        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    } }

Use a string in multiple conditions in a PHP IF statement

$mystring is declared as follows:

$mystring = "&& substr($losuj, 0, 1) == 'D' || substr($losuj, 0, 1) == 'K'";

I'd like to use $mystring (it's created by a loop earlier in my code) in the following statement:

while($row = mysql_fetch_array($wynik)) {
    $losuj = $row['wyraz'];
    if (!in_array($losuj, $losowe) 
    $mystring ){.......do something}
}

Is it even doable in PHP? I've tried echoing it normally, with {'$mystring'}, but I guess you can't interrupt loop statement just like that?

I'm trying to make a console based battleship game but I don't know how to set the ships

        int randRow = rand.nextInt(rows-4);
        int randColumn = rand.nextInt(columns); 

here i check if my new ship doesn't interfere with my first ship. the entire array is filled with objects which contain a '.' char except for where the first ship is

        if(field[randRow][randColumn].value == '.' &&
                field[randRow][randColumn+1].value == '.' &&
                field[randRow][randColumn+2].value == '.' &&
                field[randRow][randColumn+3].value == '.'){
        field[randRow][randColumn] = new Square('b');
        field[randRow+1][randColumn] = new Square('b');
        field[randRow+2][randColumn] = new Square('b');
        field[randRow+3][randColumn] = new Square('b');

        }else{

if my new ship doesn't fit i want to retry with different random values but i don't know how.

        }

Is Zero less than zero?

assume I get a value i=0; then will the following variable leftsmaller be true?

 while(i<=0 || arr[i]<arr[j])
    leftsmaller = true;

performSegueWithIdentifier in if statement isn't working

I have made a funtion for my IBAction with a performSegueWithIdentifier and it worked perfectly fine until now it always shows the alert but it also perform the segue to the next view. I don't know what I have done wrong. The identifier is wright in the MainView. I can't finde any answer to solve this.

@IBOutlet weak var backgroundImageView: UIImageView!
@IBOutlet weak var logInPassword: UITextField!
@IBAction func registerButton(sender: AnyObject) {
}

@IBAction func login(sender: AnyObject) {

    if logInPassword.text!.isEmpty {
        let alertNoPasswordLogIn = UIAlertView()
        alertNoPasswordLogIn.title = "Passwort fehlt"
        alertNoPasswordLogIn.message = "Bitte geben sie ein Passwort ein!"
        alertNoPasswordLogIn.addButtonWithTitle("Ok")
        alertNoPasswordLogIn.show()
    } else {
        self.performSegueWithIdentifier("Login", sender: self)
    }
}