0

For the get_letter_from_user function, while using the while loop for validation, it keeps repeating the invalid input; I want to make sure that it is a single letter and lower case, and I want to make sure that it doesn't equal the second parameter of the function. I'm not sure what I'm doing wrong, though. (and how to get gud at coding if u have tips)

def get_text_from_user(prompt):
    return input(prompt).lower()
    
    
    
    
def get_letter_from_user(prompt, not_allowed):          
    not_allowed = ''
    
    allowed = input(prompt).lower()
    while not allowed == not_allowed or allowed.isalpha() or len(allowed) > 1:
        allowed = str(input('Invalid letter, try again:'))
        
    
    return allowed
    
    
    
    
def main():
    text = get_text_from_user("Enter some text: ")
    ltr1 = get_letter_from_user("Enter a letter: ", '')  
    ltr2 = get_letter_from_user("Enter another letter: ", ltr1)  
    new_text = text.replace(ltr1,ltr2)
    print("The new text is", new_text)
    
    
if __name__ == "__main__":
    main()
Fran_CS
  • 23
  • 6
  • Welcome to Stack Overflow. Where the code says `while not allowed == not_allowed or allowed.isalpha() or len(allowed) > 1:`, exactly what do you intend for this to mean? What needs to be true in order for the loop to run? Also, please read [ask] and [mre]. If the question is about a specific loop doing the wrong thing in a specific function, then **don't** show us other functions; **do** show us hard-coded input that causes the loop to do the wrong thing. – Karl Knechtel Oct 24 '22 at 23:04
  • "(and how to get gud at coding if u have tips)" Please note that this is **not a discussion forum** and we don't work anything at all like that. Try https://reddit.com/r/learnpython instead. – Karl Knechtel Oct 24 '22 at 23:06
  • I was using the while loop wrong, and other the functions was to give better insight into the entire program. – Fran_CS Oct 25 '22 at 00:07
  • It's not clear to me: what exactly did you change after reading the answers? What was your misconception? Questions like this one are generally either not suitable for the site, or else the underlying issue is a common one that has a duplicate; but I want to be able to direct you to the correct duplicate. – Karl Knechtel Oct 25 '22 at 01:08
  • So I wanted my input to keep repeating if there was a mistake, for example if I inputted a number, or if the paramater *prompt* equal to *not_allowed* then it would say "Invalid Try Again', I just used the while loops incorrectly. So in the end with the help of the replies I typed *while allowed == not_allowed or len(allowed) > 1 or not allowed.isalpha():* My mistake was that the code always kept repeating no matter what I typed. I thought of while loops as if they were if statements. – Fran_CS Oct 25 '22 at 01:24

3 Answers3

0

To replace a letter or sequence of letters in a string, you might want to take a look at the string.replace() function:

text = input('Enter some text: ')
find = input('Enter a letter to replace: ')
replace_with = input(f'Enter a letter to replace \'{find}\' with: ')
replaced = text.replace(find, reolace_with)
print('The new text is:', replaced)
definite_d
  • 26
  • 5
0

To add another little detail because you asked how to get better at coding:

I would never make a function with a parameter that is immediately changed to an empty string. Like:

def get_letter_from_user(prompt, not_allowed):
    not_allowed = ''

Rather use a default value like this:

def get_letter_from_user(prompt, not_allowed=''):
    ...
0

Suggestion for the function:

def get_letter_from_user(prompt, not_allowed):

    allowed = input(prompt).lower()
    while allowed == not_allowed or len(allowed) > 1:
        print('not_allowed:',not_allowed)
        allowed = str(input('Invalid letter, try again:'))

    return allowed


ltr1 = get_letter_from_user("Enter a letter: ", '')  
ltr2 = get_letter_from_user("Enter another letter: ", ltr1)  

Sample output:

Enter a letter: d
Enter another letter: d
not_allowed: d
Invalid letter, try again:d
not_allowed: d
Invalid letter, try again:a
kaksi
  • 88
  • 6
  • Thank you I added *while allowed == not_allowed or len(allowed) > 1 or not allowed.isalpha():* To make everything worked, I had a dumb misconception of how while loops worked, I just realized how simple my mistake was. – Fran_CS Oct 24 '22 at 23:44