Skip to content

Instantly share code, notes, and snippets.

@miquelvir
Created March 16, 2025 21:58
Show Gist options
  • Save miquelvir/edcd3aeafe07bbe5680352b79eb80562 to your computer and use it in GitHub Desktop.
Save miquelvir/edcd3aeafe07bbe5680352b79eb80562 to your computer and use it in GitHub Desktop.
devQuest - 16/03/2025 22:57

Exercise 1

Write a program that takes character inputs from the user until an empty input is given. For each character, determine if it's lowercase, uppercase, a number, or a symbol, and print an appropriate message. After the loop terminates, print a goodbye message.

Teacher solution

# Request once before entering the loop
char = input('Enter a character (ENTER to exit): ')

while char != "":  # the empty string "" is ENTER (NOT to be confused with one space " ")
  if 'a' <= char <= 'z':
      print('The letter', char, 'is lowercase')
  elif 'A' <= char <= 'Z':
      print('The letter', char, 'is uppercase')
  elif '0' <= char <= '9':
      print(char, 'is a number')
  else:
      print(char, 'is a symbol')
  char = input('Enter a character (ENTER to exit): ')   # request again at the end of the loop

print('You did not enter any character. BYE!')

Test cases

These test cases have been created with AI, cross check anything important.

Mixed characters

Input

a
B
1
!

Expected output

Enter a character (ENTER to exit): The letter a is lowercase
Enter a character (ENTER to exit): The letter B is uppercase
Enter a character (ENTER to exit): 1 is a number
Enter a character (ENTER to exit): ! is a symbol
Enter a character (ENTER to exit): You did not enter any character. BYE!

Empty input

Input


Expected output

Enter a character (ENTER to exit): You did not enter any character. BYE!

First character taken

Input

abc

Expected output

Enter a character (ENTER to exit): The letter a is lowercase
Enter a character (ENTER to exit): You did not enter any character. BYE!

Explanation

This explanation has been created with AI, cross check anything important.

The code first asks the user to input a character. This input is stored in the variable char. The program will continue to ask for inputs until the user simply presses 'Enter' without typing any character.

char = input('Enter a character (ENTER to exit): ')```

The `while` loop continues as long as the variable `char` is not an empty string (`""`). An empty string is what you get when the user presses 'Enter' without typing anything.

```python
while char != "":```

Inside the `while` loop, a series of `if`, `elif`, and `else` statements checks what kind of character the user entered.

```python
if 'a' <= char <= 'z':
  elif 'A' <= char <= 'Z':
  elif '0' <= char <= '9':
  else:```

The first `if` statement checks if the character is a lowercase letter (between 'a' and 'z'). If it is, a message is printed saying that the letter is lowercase.

```python
if 'a' <= char <= 'z':
    print('The letter', char, 'is lowercase')```

The `elif` statement checks if the character is an uppercase letter (between 'A' and 'Z'). If it is, a message is printed saying that the letter is uppercase.

```python
elif 'A' <= char <= 'Z':
    print('The letter', char, 'is uppercase')```

The next `elif` statement checks if the character is a digit (between '0' and '9'). If it is, a message is printed saying that it is a number.

```python
elif '0' <= char <= '9':
    print(char, 'is a number')```

The `else` statement is executed if the character is not a lowercase letter, an uppercase letter, or a digit. This means it must be a symbol (like !, @, #, etc.), and a message is printed saying that.

```python
else:
    print(char, 'is a symbol')```

At the end of the `while` loop, the program asks the user to enter another character. This new input updates the `char` variable, and the loop starts again from the beginning. This is crucial because without this, the loop would run forever.

```python
char = input('Enter a character (ENTER to exit): ')```

If the user enters an empty string (by pressing 'Enter' without typing anything), the `while` loop condition becomes false, and the loop stops. Then, the program prints a 'goodbye' message.

```python
print('You did not enter any character. BYE!')```

## Practice exercises

> These exercises have been created with AI, cross check anything important.

- `similar` Write a program that continuously prompts the user for a day of the week (e.g., 'Monday', 'Tuesday'). If the input is 'Saturday' or 'Sunday', print 'It's the weekend!'. Otherwise, print 'It's a weekday.'. The program should exit when the user enters 'exit'.
- `moderate`  Create a program that asks the user to input a series of numbers. The program should calculate and print the sum of the positive numbers and the product of the negative numbers entered. The program should terminate when the user enters 0.
- `challenge`  Develop a program that simulates a basic calculator. The program should repeatedly ask the user for an operation (+, -, *, /) and two numbers. Perform the calculation and print the result. Include error handling for invalid operations (print 'Invalid operation') and division by zero (print 'Division by zero error'). The program should exit if the user enters 'exit' as the operation.
- `challenge`  Design a program that takes integer inputs from the user until the user types 'done'. Store these integers in a list. After the loop finishes, calculate and print the mean, median, and mode of the numbers entered. If no numbers are entered, print an appropriate message. Handle potential errors such as non-integer input gracefully.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment