销毁时更改数据类型

时间:2019-05-26 05:28:38

标签: python destructuring

采用以下代码:

session_start();// Starting Session

//if session exit, user nither need to signin nor need to signup
if(isset($_SESSION['login_id'])){
  if (isset($_SESSION['pageStore'])) {
      $pageStore = $_SESSION['pageStore'];
header("location: $pageStore"); // Redirecting To Profile Page
    }
}

//Register progess start, if user press the signup button
if (isset($_POST['signUp'])) {
if (empty($_POST['fullName']) || empty($_POST['email']) || empty($_POST['newPassword'])) {
echo "Please fill up all the required field.";
}
else
{

$fullName = $_POST['fullName'];
$email = $_POST['email'];
$password = $_POST['newPassword'];
$hash = password_hash($password, PASSWORD_DEFAULT);

// Make a connection with MySQL server.
include('config.php');

$sQuery = "SELECT id from account where email=? LIMIT 1";
$iQuery = "INSERT Into account (fullName, email, password) values(?, ?, ?)";

// To protect MySQL injection for Security purpose
$stmt = $conn->prepare($sQuery);
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($id);
$stmt->store_result();
$rnum = $stmt->num_rows;

if($rnum==0) { //if true, insert new data
          $stmt->close();

          $stmt = $conn->prepare($iQuery);
          $stmt->bind_param("sss", $fullName, $email, $hash);
          if($stmt->execute()) {
        echo 'Register successfully, Please login with your login details';}
        } else { 
       echo 'Someone already registered with this discord username';
     }
$stmt->close();
$conn->close(); // Closing database Connection
}
}

?> 
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Register</title>
    <link rel="stylesheet" href="auth.css">
</head>
<body>
 <div class="rlform">
  <div class="rlform rlform-wrapper">
   <div class="rlform-box">
    <div class="rlform-box-inner">
     <form method="post" oninput='validatePassword()'>
      <p>Let's create your account</p>

     <div class="rlform-group">
      <label>Username</label>
      <input type="text" name="fullName" class="rlform-input" required>
     </div>

     <div class="rlform-group">                 
      <label>Discord Username and Identifier</label>
      <input type="text" name="email" class="rlform-input" placeholder = "EX: DiscordUser#1234" required>
     </div>

     <div class="rlform-group">                 
      <label>Password</label>
      <input type="password" name="newPassword" id="newPass" class="rlform-input" required>
     </div>

     <div class="rlform-group">
      <label>Confirm password</label>
      <input type="password" name="conformpassword" id="conformPass" class="rlform-input" required>
     </div>

      <button class="rlform-btn" name="signUp">Sign Up
      </button>

      <div class="text-foot">
       Already have an account? <a href="login.php">Login</a>
      </div>
     </form>
    </div>
   </div>
  </div>
 </div>

    <script>
        function validatePassword(){
  if(newPass.value != conformPass.value) {
    conformPass.setCustomValidity('Passwords do not match.');
  } else {
    conformPass.setCustomValidity('');
  }
}
    </script>
</body>
</html>

有没有办法在解构时更改字符串的数据类型?我希望以下类似的方法可以工作,但不幸的是,它不起作用:

nums = ["1", "2", "3"]
one, two, three = nums
print("Sum:", one+two+three)  # >> Sum: 123

我知道我可以通过执行nums = ["1", "2", "3"] int(one), int(two), int(three) = nums print("Sum:", one+two+three) # Expected output >> Sum: 6 等来简单地更改数据类型...但是我只是想知道在解构赋值表达式本身中是否可以做类似的事情?

2 个答案:

答案 0 :(得分:1)

您可以执行以下操作。 map函数会将作为第一个参数传递的函数应用于列表中所有或可迭代的值。

注意:map很懒。结果将仅在迭代期间获得。您可以找到更多详细信息here

nums = map(int, ["1", "2", "3"])

print(sum(nums)) # 6

现在num中的所有值都将为int。

答案 1 :(得分:1)

您可以使用map内置函数将所有元素转换为int 通过对其应用类型转换函数int

从文档中

  

map(函数,可迭代,...)
  返回一个将函数应用于所有iterable的迭代器,并产生结果。

然后您可以使用更新的迭代器来分配元素

In [234]: nums = ["1", "2", "3"]                                                                                                                                                    

In [235]: one, two, three = map(int,nums)                                                                                                                                           

In [236]: print("Sum:", one+two+three)                                                                                                                                              
Sum: 6

请注意,这与执行以下操作相同,在此过程中,我们遍历nums并将每个字符串类型转换为int。 map是该操作的简写。

In [254]: nums = ["1", "2", "3"]                                                                                                                                                    

In [255]: one, two, three = [int(num) for num in nums]                                                                                                                              

In [256]: print("Sum:", one+two+three)                                                                                                                                              
Sum: 6