Skip to content

Instantly share code, notes, and snippets.

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

Exercise 1

Write a program that continuously prompts the user to enter a character. The program should then determine if the character is lowercase, uppercase, a number, or a symbol, and print an appropriate message. The program should exit when the user presses ENTER without entering a character.

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.

Lowercase letter input

`INPUT```` a


`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!

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

Uppercase letter input

`INPUT```` A


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

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

Number input

`INPUT```` 1


`OUTPUT````
Enter a character (ENTER to exit): 1 is a number
Enter a character (ENTER to exit): You did not enter any character. BYE!

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

Symbol input

`INPUT```` !


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

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

Empty input

`INPUT````


`OUTPUT````
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 program first asks the user to enter a character and stores it in the variable char. The program will continue to run until the user just presses 'Enter' without typing a character.python char = input('Enter a character (ENTER to exit): ')

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

The while loop continues as long as the user enters some character and presses 'Enter'. If the user just presses 'Enter', then char will be assigned an empty string "", and the loop will terminate.python while char != "":

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

Inside the loop, the program checks what kind of character the user entered. The first if statement checks if the character char is a lowercase letter (between 'a' and 'z' in the alphabet).python if 'a' <= char <= 'z': print('The letter', char, 'is lowercase')

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

The elif statement checks if the character char is an uppercase letter (between 'A' and 'Z').python elif 'A' <= char <= 'Z': print('The letter', char, 'is uppercase')

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

The next elif statement checks if the character char is a digit (between '0' and '9').python elif '0' <= char <= '9': print(char, 'is a number')

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

The else block executes if the character is not a lowercase letter, an uppercase letter, or a digit. In this case, it's considered a symbol.python else: print(char, 'is a symbol')

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

At the end of the loop, the program asks the user to enter another character. The loop then repeats with the new character entered by the user.python char = input('Enter a character (ENTER to exit): ')

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

After the loop finishes (when the user enters an empty string), 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.

  • Write a program that continuously prompts the user to enter a number. If the number is positive, print 'Positive'. If the number is negative, print 'Negative'. If the number is zero, print 'Zero'. The program should exit when the user enters '100'.> These exercises have been created with AI, cross check anything important.

  • + Write a program that takes character inputs from the user until the user enters 'quit'. The program should categorize each character as a vowel (a, e, i, o, u - case-insensitive), a consonant, a digit, or a special character. Print the category of each entered character.> These exercises have been created with AI, cross check anything important.

  • ++ Create a program that continuously prompts the user to enter a string. The program should analyze the string and determine if it is a palindrome (reads the same forwards and backward). The analysis should be case-insensitive, and ignore spaces and punctuation. Print whether each input string is a palindrome or not. The program exits when the user enters 'exit'.> These exercises have been created with AI, cross check anything important.

  • ++ Develop a program that takes integer inputs from the user until the user enters '-1'. The program should keep track of the sum of all even numbers and the product of all odd numbers entered. After the loop, print both the sum of even numbers and the product of odd numbers. If no even or odd numbers are entered, then print a message indicating that no even/odd number was entered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment