Need help with python! It wont output right. and any alterations break the code.So I have the code done below but I dont know how to have it output a certain way.Expected output:Enter your name: PhilIn text name is: P h i lIn ASCII decimal: 80 104 105 108 In ASCII binary: 01010000 01101000 01101001 01101100Enter your age: 71As a string "71": 7 1In ASCII decimal: 55 49In ASCII binary: 0110111 0110001As an int variable: 71In numeric binary: 1000111Code (need help here): print(f"\nName: {name}")# Loop through each letter:for letter in name: ascii_val = ord(letter) # decimal ASCII binary_val = format(ascii_val, '08b') # 8-bit binaryprint("Character")print(f"{letter:9}")print("ASCII Decimal")print(f"{ascii_val:13}")print("ASCII Binary")print(f"{binary_val}") # ageage_str = input("\nEnter your age: ")age_int = int(age_str)print(f"\nAge as string: {age_str}")print(f"Age as int: {age_int}")print(f"Age in ASCII (per digit):")for digit in age_str: ascii_val = ord(digit) binary_val = format(ascii_val, '08b') print(f"'{digit}' -> Decimal: {ascii_val}, Binary: {binary_val}")
Need help with python! It wont output right. and any alterations break the code.
So I have the code done below but I dont know how to have it output a certain way.
Expected output:
Enter your name: Phil
In text name is: P h i l
In ASCII decimal: 80 104 105 108
In ASCII binary: 01010000 01101000 01101001 01101100
Enter your age: 71
As a string "71": 7 1
In ASCII decimal: 55 49
In ASCII binary: 0110111 0110001
As an int variable: 71
In numeric binary: 1000111
Code (need help here):
print(f"\nName: {name}")
# Loop through each letter:
for letter in name:
ascii_val = ord(letter) # decimal ASCII
binary_val = format(ascii_val, '08b') # 8-bit binary
print("Character")
print(f"{letter:9}")
print("ASCII Decimal")
print(f"{ascii_val:13}")
print("ASCII Binary")
print(f"{binary_val}")
# age
age_str = input("\nEnter your age: ")
age_int = int(age_str)
print(f"\nAge as string: {age_str}")
print(f"Age as int: {age_int}")
print(f"Age in ASCII (per digit):")
for digit in age_str:
ascii_val = ord(digit)
binary_val = format(ascii_val, '08b')
print(f"'{digit}' -> Decimal: {ascii_val}, Binary: {binary_val}")
Step by step
Solved in 2 steps with 4 images









