Day 5 of Angela Yu's Python Pro bootcamp involved for loops which in Python, the syntax is:
for [variable name] in [variable-name]:
I did various interactive exercises involving calculating the average of high scores and calculating the sum of even numbers from 1 to 100 but the main project I built today was a password generator, which lets the user input how many numbers, letters and symbols they want in their password before generating a random password with the inputted letters, numbers and symbols.
import random
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
symbols = ['!', '#', '$', '%', '&', '(', ')', '*', '+']
print("Welcome to the PyPassword Generator!")
nr_letters = int(input("How many letters would you like in your password?\n"))
nr_symbols = int(input(f"How many symbols would you like?\n"))
nr_numbers = int(input(f"How many numbers would you like?\n"))
password_list = []
for char in range(1, nr_letters + 1):
password_list.append(random.choice(letters))
for char in range(1, nr_symbols + 1):
password_list += random.choice(symbols)
for char in range(1, nr_numbers + 1):
password_list += random.choice(numbers)
random.shuffle(password_list)
password = ""
for char in password_list:
password += char
print(f"Your password is: {password}")
I have mentioned previously that what I love about Python is its simple syntax and how it has a lot of built-in functions like random.choice() and random.shuffle() that makes it simple to randomly generate characters. In C#, these functions are not built in and the programmer would have to create a class with multiple lines of code to randomly shuffle the list and then reference that class. After trying to code the equivalent of the above code in C#, it prints out a random list of numbers instead of numbers, letters and symbols.
string[] letters = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
string[] numbers = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
string[] symbols = { "!", "#", "$", "%", "£", "&", "*", "+" };
Random random = new Random();
Console.WriteLine("Welcome to the C# Password Generator!");
Console.WriteLine("How many letters would you like in your password?");
int nr_letters = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("How many symbols would you like in your password?");
int nr_symbols = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("How many numbers would you like in your password?");
int nr_numbers = Convert.ToInt16(Console.ReadLine());
List<string> password_list = new List<string>();
int letterindex = random.Next(letters.Length);
int symbolindex = random.Next(symbols.Length);
int numberindex = random.Next(numbers.Length);
foreach (int passchar in Enumerable.Range(1, nr_letters + 1))
{
password_list.Add(letterindex.ToString());
}
foreach (int passchar in Enumerable.Range(1, nr_symbols + 1))
{
password_list.Add(symbolindex.ToString());
}
foreach (int passchar in Enumerable.Range(1, nr_numbers + 1))
{
password_list.Add(numberindex.ToString());
}
var shuffled = password_list.OrderBy(_ => random.Next()).ToList();
string password = "";
foreach (string passchar in password_list)
{
password += passchar;
}
string passwordFormat = String.Format("Your password is " + password);
Console.WriteLine(passwordFormat);
Since there is no visualizer for C# like Python has with Python Tutor and Thonny, I cannot see why it only prints out numbers instead of numbers, letters and symbols. I will continue with Day 6 next week.
Top comments (0)