Python Forum
hi need help to make this code work correctly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hi need help to make this code work correctly
#1
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])
buran write Nov-20-2023, 09:25 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
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)
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#4
# 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
Larz60+ write Nov-20-2023, 01:39 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed this time. Please use BBCode tags on future posts.
Reply
#5
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]}')
Reply
#6
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why can I not see the code correctly in Python IDLE.? Trump 8 720 Apr-04-2024, 07:47 AM
Last Post: jonesphedra
  newbie question - can't make code work tronic72 2 696 Oct-22-2023, 09:08 PM
Last Post: tronic72
  Cleaning my code to make it more efficient BSDevo 13 1,381 Sep-27-2023, 10:39 PM
Last Post: BSDevo
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,355 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Beginner: Code not work when longer list raiviscoding 2 832 May-19-2023, 11:19 AM
Last Post: deanhystad
  how to make bot that sends instagram auto password reset code kraixx 2 1,385 Mar-04-2023, 09:59 PM
Last Post: jefsummers
  Why doesn't this code work? What is wrong with path? Melcu54 7 1,810 Jan-29-2023, 06:24 PM
Last Post: Melcu54
  Code used to work 100%, now sometimes works! muzicman0 5 1,450 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  color code doesn't work harryvl 1 896 Dec-29-2022, 08:59 PM
Last Post: deanhystad
  Make code non-blocking? Extra 0 1,142 Dec-03-2022, 10:07 PM
Last Post: Extra

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020