I meant this, Thank you Very much
int score=0;
int len=password.Length;
**if(password=="1234")**
** {**
** score=0;**
** }**
else if (len >= minLength)
{
score++;
}
if (Tools.Contains(password, uppercase))
{
score++;
}
if (Tools.Contains(password, lowercase))
{
score++;
}
if(Tools.Contains(password, digits))
{
score++;
}
if(Tools.Contains(password, specialChars))
{
score++;
}
Console.WriteLine(score);
I did it like this whats wrong with this?
Why do you think its wrong?
you know only check if password
is '1234'
, you forgot to check if password
variable equals 'password'
string
For one, you check at the start of the password is equal to 1234 (let’s assume you also checked for password or all excluded words for now) and set the score to 0.
That’s fine, but you should take the score there and not do all the other password checks because they will give a score of at least 1 otherwise (1 for digit in the case of 1234 or 1 for lowercase in the case of password). That may have been why you was trying to do with the first else if?
With regards to checking if the password is equal to an excluded words then the easiest way is probably to have a collection of the excluded words and then check if the password is in that. This allows you to add to it or remove from it easily Vs writing extra if statements for each possible excluded word.
The Tools.Contains() method won’t work in my own independent environment when I copy and paste the code from my ‘Password Checker’ project into my own desktop Visual Studio.
The methods worked fine when I ran it on the website.
Does anyone know if the Tools.Contains(target, list) method was one created from scratch by Codecademy specifically for the ‘Password Check’ exercise?
link to this questions origin: https://www.codecademy.com/courses/learn-c-sharp/projects/csharp-password-checker
if (Tools.Contains(password, uppercase) == true)
{
score++;
}
if (Tools.Contains(password, lowercase) == true)
{
score++;
}
if (Tools.Contains(password, digits) == true)
{
score++;
}
if (Tools.Contains(password, specialChars) == true)
{
score++;
}
Hey @board3621771149,
First check out @midlindner’s response here: C# Password Check Exercise - #14 by midlindner.
If that doesn’t work for you, then try answering this second question:
What do you mean by “won’t work”?
- Is there a specific error that shows up?
- Does the program run but the score is wrong?
- Something else…?
It seems like a number of people, when they try the project on their own computers, run into a similar issue. This is probably because I didn’t clearly explain Tools.Contains()
.
Tools.Contains()
is a custom tool provided by us. It’s defined in a separate file Tools.cs.
I’ve added some clarification to the project - hopefully it helps future learners avoid this issue!
I am stuck in this project as well and don’t know what else to do:
using System;
namespace PasswordChecker
{
class Program
{
public static void Main(string args)
{
// setting password standards
int minLength = 8;
string uppperCase = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
string lowerCase = “abcdefghijklmnopqrstuvwxyz”;
string digits = “0123456789”;
string specialChars = “`~!@#$%^&*()-+”;
// asking for the password
Console.Write("Enter password: ");
string userPassword = Console.ReadLine();
// BEGIN evaluating the password
int score = 0;
// first, min length
if (userPassword.Length >= minLength){
score++;
}
// then, uppercase
if (Tools.Contains(userPassword, upperCase)){
score++;
}
Console.WriteLine(score);
}
}
}
Error message: Program.cs(29,40): error CS0103: The name ‘upperCase’ does not exist in the current context [/home/ccuser/workspace/csharp-password-checker/PasswordChecker.csproj]
The build failed. Fix the build errors and run again.
Final note: I’ve made tweaks to see what else I can do and clearly the string upperCase is created above. Thoughts?
I can’t seem to figure out the contains tool thing even after reading all these comments, any help?
Hi guys,
In this exercise, a bunch of spaces is supposed to return a score of 0, but if I type in 8 0’s or more it complies with the minLength requirement and scores a 1. This is obviously not ideal to have people create a password with just one number, letter or spaces repeated multiple times.
How do I have users still score 0, regardless of whether it satisfies the minLength requirement or not?
Thanks!
You would need some kind of check to see if all the characters are the same
Its because you spelt upperCase wrong when you declared it. You spelt it with an extra p.
then in your if statement you spelt it correctly.
So change this:
string uppperCase = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
To this:
string upperCase = “ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
and it should work
the Tools.Contains method wont work in Visual Studio unless you add it as a class.
You can do this by selecting the project tab at the top of Visual Studio, then select add Class.
You will want to name this class as Tools.cs
Then select the class by clicking on Tools.cs from the Solution explorer on the right hand side of visual studio
Inside of the class paste the following code.
using System;
using System.Collections.Generic;
using System.Text;
namespace Codecademy
{
public class Tools
{
public Tools()
{
}
public static bool Contains(string target, string list)
{
return target.IndexOfAny(list.ToCharArray()) != -1;
}
public static void ContainsTest()
{
string loudWord = "LASDAD";
string quietWord = "pssst";
string mixedWord = "lkaAWEkasfkEW";
string uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string lowercase = "abcdefghijklmnopqrstuvwxyz";
Console.WriteLine("Running \"tests...\"");
Console.WriteLine($"Should be true: {Contains(loudWord, uppercase)}");
Console.WriteLine($"Should be false: {Contains(loudWord, lowercase)}");
Console.WriteLine($"Should be false: {Contains(quietWord, uppercase)}");
Console.WriteLine($"Should be true: {Contains(quietWord, lowercase)}");
Console.WriteLine($"Should be true: {Contains(mixedWord, uppercase)}");
Console.WriteLine($"Should be true: {Contains(mixedWord, lowercase)}");
}
}
}
You also need to add “using Codecademy;” to the Program.cs underneath the ‘using system;’ part of the text
I’ve included a little more below than i needed to in the hope that it would make it clearer.
using System;
using Codecademy;
namespace PasswordChecker
{
class Program
{
public static void Main(string[] args)
If this has helped click the like button.
Ps I’m a newb, be kind
hi! @nduckwiler, can you please be so kind to explain, how do I actually copy the Tools.cs file? I can copy the contents of the open Program.cs file, but how do I do it with the Tools.cs? I don’t see it anywhere, including the opportunity to open it.
hello
how do you add this :
"If you want an extra challenge, try adding these requirements:
- If the password is
password
or1234
, give it a score of 0".
i have tried some ways but cant. can anybody help me please
What have you tried? can you show us some of your attempts?
I used this instead, and it worked fine without using tools.contains
if (upperCase.Any(password.Contains))
{
score++;
}
if (lowerCase.Any(password.Contains))
{
score++;
}
if (numbers.Any(password.Contains))
{
score++;
}
if (specialChar.Any(password.Contains))
{