Python Forum
hi need help to make this code work correctly - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: hi need help to make this code work correctly (/thread-41153.html)



hi need help to make this code work correctly - atulkul1985 - Nov-20-2023

hi need help to make this code work correctly
Please excuse me if its too simple problem to ask on this tread but i am beginner and very basic learner level of coder

So my need from this code is to get the exact alphabet from the number assigned to it

means if i press 3 than it should show me L

currently its not giving the errors but is is showing AA

Pls help

Here is the code

name = "ATUL"
letter_0 = "A"
letter_1 = "T"
letter_2 = "U"
letter_3 = "L"
print("hello, " + name)
a = input("enter the number to find the letter's position")
print("your name is", "A",  name[0])
b = input("enter the number to find the letter's position")
print("your name is", "T", name[1])
c = input("enter the number to find the letter's position")
print("your name is", "U" , name[2])
d = input("enter the number to find the letter's position")
print("your name is", "L" , name[3])



RE: hi need help to make this code work correctly - DPaul - Nov-20-2023

If you want to find the letter position in the "name" variable,
you might want to look at the name.index(a...) function.
Should do the trick. (watch out for upper-lowercase)
Paul


RE: hi need help to make this code work correctly - rob101 - Nov-20-2023

I'm not entirely sure what your code is suppose to show, but maybe this will help you to understand the index positions of letters in a string object.

name = input("What is your name? ")

for index, letter in enumerate(name):
    print("Your name is", index, letter)



RE: hi need help to make this code work correctly - EdwardMatthew - Nov-20-2023

# Define the name
name = "ATUL"

# Greet the user
print("Hello, " + name)

# Ask the user for a number and find the corresponding letter
number = int(input("Enter the number to find the letter's position (0-3): "))

# Check if the number is within the valid range
if 0 <= number < len(name):
    # Print the corresponding letter
    print("The letter at position", number, "is:", name[number])
else:
    print("Invalid number. Please enter a number between 0 and 3.")
This code does the following:

1. Asks the user to input a number between 0 and 3.
2. Check if the number is within the range of indices for the name "ATUL".
3. Prints the letter at the specified position in the name if the number is valid.
4. Gives an error message if the number is out of range.

I hope this helps


RE: hi need help to make this code work correctly - Pedroski55 - Nov-20-2023

You should have fun whilst you Python!

name = input('Enter a name, any name ... ')
print(f"hello {name}!')
Output:
Beelzeebub
possibilities = [i for i in range(len(name))]
letter_dict = {index: letter for index, letter in enumerate(name)}
for key in letter_dict.keys():
    print(f'key is {key}, value is {letter_dict[key]}')
To control an evil-doer, spell his name backwards:

print('If I spell your name backwards I will have magic power over you!!')
for i in range(len(name)-1, -1, -1):
      print(name[i], sep='', end='')
Output:
bubeezleeB
Boring stuff:

while True:
    number = input("enter the number to find the letter's position, enter q to quit. ")
    if number == 'q':
        print('Goodbye sweet child!')
        break
    while not int(number) in possibilities:
        number = input(f"The number is not in the acceptable range 0 to {len(name) - 1}, please try again. ")        
    for key in letter_dict.keys():
        if key == int(number):
            print(f'Number {number} letter is {letter_dict[key]}')



RE: hi need help to make this code work correctly - deanhystad - Nov-20-2023

It sounds like you want to make a mapping between letters and numbers. This could be really simple:
name= input("Enter Name: ")

print(name[int(input("Number: "))])
If you enter "ATUL" this creates a map where 0 = A, 1 = T, 2 = U, 3 = L.

If you want to map different numbers to the letters, you can make a dictionary.
name = input("Enter Name: ")
alphabet = {}
for letter in name:
    alphabet[int(input(f"Enter number for {letter}: "))] = letter


print(alphabet[int(input("Number: "))])
Output:
Enter Name: ATUL Enter number for A: 22 Enter number for T: 17 Enter number for U: 11 Enter number for L: 6 Number: 17 T