How do I extend to convert an integer to a character by using the 'chr()' function, and output that character

Question:

I’m having trouble in the 3rd part of my Scripting homework.
1.23 LAB: Warm up: Variables, input, and type conversion
(1) Prompt the user to input an integer between 32 and 126, a float, a character, and a string, storing each into separate variables. Then, output those four values on a single line separated by a space.(2) Extend to also output in reverse. (3) Extend to convert the integer to a character by using the ‘chr()’ function, and output that character.
It seems to come up fine when I run the program but when I submit for grading I get 0 points. Here’s my code:

user_int = int(input('Enter integer (32 - 126):n'))
user_float = float(input('Enter float:n'))
user_char = input('Enter character:n')
user_str = input('Enter string:n')

print(str(user_int) + " " + str(user_float) + " " + str(user_char) + " " + user_str)
print(str(user_str) + " " + str(user_char) + " " + str(user_float) + " " + str(user_int))
print(str(chr(user_int)) + " " + str(user_float) + " " + str(user_char) + " " + user_str)
Asked By: irishsara

||

Answers:

A few remarks on your code.

one:

print(str(user_int) + " " + str(user_float) + " " + str(user_char) + " " + user_str)

It is not necessary to cast user_char to str. It is already a string.

print(str(user_int) + " " + str(user_float) + " " + user_char + " " + user_str)

two:

print(str(user_str) + " " + str(user_char) + " " + str(user_float) + " " + str(user_int))

Again you have cast usr_char (and user_str), to str and it isn’t necessary as they are already strings and don’t require casting to str.

print(user_str + " " + user_char + " " + str(user_float) + " " + str(user_int))

three:

And as I remarked in my comment above, (and I may be wrong), I think the specification only requires you to print out the character decoded from the user_int.

print(chr(user_int))

The changes I suggested could be rendered like the following.

user_int = int(input('Enter integer (32 - 126):n'))
user_float = float(input('Enter float:n'))
user_char = input('Enter character:n')
user_str = input('Enter string:n')

print(user_int, user_float, user_char, user_str)
print(user_str, user_char, user_float, user_int)
print(chr(user_int))

(Note that I didn’t add spaces between the items as printing a comma separated list of items adds a space between each item. Also, you can print the user_int and user_float without casting them to str and python will accept them. It is necessary to cast them to str if you are concatenating them to (spaces in your approach). (However, it seems your instructor wants you to cast them, so you should probably use your approach adding spaces between each item in your print statements and don’t use my approach that uses a comma separated list).

Running this program, I got this sample result below.

Enter integer (32 - 126):
122
Enter float:
3.1415
Enter character:
B
Enter string:
Tom likes watermelon
122 3.1415 B Tom likes watermelon
Tom likes watermelon B 3.1415 122
z
Answered By: Chris Charley

This worked and satisfied all 3 portions of the question.

user_int = int(input('Enter integer (32 - 126):n'))
user_float = float(input('Enter float:n'))
user_char = input('Enter character:n')
user_string = input('Enter string:n')

print(user_int, user_float, user_char, user_string)
print(user_string, user_char, user_float, user_int)
print(user_int, 'converted to a character is', chr(user_int))

Hopefully this helps!

Answered By: KCfan54738

A year later, and it still works like a charm. Thanks, KCfan54738.

Answered By: LLKone1987
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.