73.2k views
3 votes
Write a while loop that reads five characters and prints the ASCII value of each. Enter a character: a The ASCII value is 97 Enter a character: A The ASCII value is 65 Enter a character: ! The ASCII value is 33 Enter a character: # The ASCII value is 35 Enter a character: ? The ASCII value is 63

1 Answer

5 votes

Final answer:

To write a while loop that reads five characters and prints the ASCII value of each, use the input() function to prompt the user to enter a character and the ord() function to get the ASCII value. The loop continues until count reaches 5.

Step-by-step explanation:

To write a while loop that reads five characters and prints the ASCII value of each, you can use the input() function in Python. Here's an example:

count = 0
while count < 5:
char = input('Enter a character: ')
ascii_value = ord(char)
print(f'The ASCII value is {ascii_value}')
count += 1

In this example, the input() function is used to prompt the user to enter a character, and the ord() function is used to get the ASCII value of that character. The while loop continues until count reaches 5, and it prints the ASCII value of each character.

User Diego Allen
by
8.8k points