Weekly Challenge #2 – Basic Word Game – Solutions

Note: This contains answers to the Basic Word Game challenge. Have a go at the challenge before coming here!

Bronze

word = input("Please enter your word: ")

word_length = len(word)
score = 0

if word_length == 3:
    score = 1
elif word_length == 4 or word_length == 5:
    score = 2
elif word_length == 6 or word_length == 7:
    score = 3
elif word_length == 8 or word_length == 9:
    score = 5

print("Your score is:", score)
Console.Write("Please enter your word: ");
string word = Console.ReadLine();

int score = 0;

if (word.Length == 3)
{
    score = 1;
}
else if (word.Length == 4 || word.Length == 5)
{
    score = 2;
}
else if (word.Length == 6 || word.Length == 7)
{
    score = 3;
}
else if (word.Length == 8 || word.Length == 9)
{
    score = 5;
}

Console.WriteLine("Your score is {0}.", score);

(Python only) – I have chosen to store the length of the word inside a variable called word_length in order to make the code more readable. You may skip this step and access the length each time by using len(word).

Silver

 

word = input("Please enter your word: ")

word_length = len(word)
score = 0

while word_length < 3 or word_length > 9:
    word = input("Invalid word, please re-enter: ")
    word_length = len(word)

if word_length == 3:
    score = 1
elif word_length <= 5:
    score = 2
elif word_length <= 7:
    score = 3
else:
    score = 5

print("Your score is:", score)
string word;
do
{
    Console.Write("Please enter your word: ");
    word = Console.ReadLine();
} while (word.Length < 3 || word.Length > 9);
            
int score = 0;

if (word.Length == 3)
{
    score = 1;
}
else if (word.Length <= 5)
{
    score = 2;
}
else if (word.Length <= 7)
{
    score = 3;
}
else
{
    score = 5;
}

Console.WriteLine("Your score is {0}.", score);

Notice that I have now simplified the If/Elif statement since I have validation to ensure that all words are between 3-9 characters long. 

Gold

word = input("Please enter your word: ").upper()

word_length = len(word)
score = 0

while word_length < 3 or word_length > 9:
    word = input("Invalid word, please re-enter: ").upper()
    word_length = len(word)

if word_length == 3:
    score = 1
elif word_length <= 5:
    score = 2
elif word_length <= 7:
    score = 3
else:
    score = 5

consonant_count = 0
vowel_count = 0
for char in word:
    if char in "AEIOU":
        vowel_count += 1
    else:
        consonant_count += 1

if consonant_count >= 4:
    score += 2
if vowel_count == 1:
    score += 2

print("Your score is:", score)
string word;
do
{
    Console.Write("Please enter your word: ");
    word = Console.ReadLine().ToUpper();
} while (word.Length < 3 || word.Length > 9);
            
int score = 0;
if (word.Length == 3)
{
    score = 1;
}
else if (word.Length <= 5)
{
    score = 2;
}
else if (word.Length <= 7)
{
    score = 3;
}
else
{
    score = 5;
}

int consonant_count = 0, vowel_count = 0;

foreach (char c in word)
{
    if ("AEIOU".Contains(c))
    {
        vowel_count++;
    }
    else
    {
        consonant_count++;
    }
}
if (consonant_count >= 4)
{
    score += 2;
}
if (vowel_count == 1)
{
    score += 2;
}
Console.WriteLine("Your score is {0}.", score);

 

A couple of things to note here

  • Line 1 & 7 (Python), Line 5 (C#) – I have converted the user’s input to upper case. This will make counting vowels/consonants easier later on.
  • Line 21 (Python), Line 28 (C#) – This loops through each character in the user’s word. This creates a temporary variable called char (Python) / c(C#) which stores the current character we are checking.
  • Line 22 (Python), Line 30 (C#) – This is a nice shortcut for checking to see whether a single character exists in a string. So if the char is “A”, we will check to see if that is in “AEIOU” (Which it is!)
  • In several places, I have used the += operator. If you’re not familiar with this, vowel_count += 1 is a shorthand way of saying vowel_count = vowel_count + 1

 

Well done if you managed to complete the platinum difficulty challenge also! As always, feel free to contact me if you’ve got any questions.