Python Forum
Basic input() function question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Basic input() function question
#1
Hi everyone,

Started programming for the first time using Python and I've gotten through all the basics and practicing putting everything together. I love it so far. A quick question for anyone that can help.

I'd like it so that when I ask a user to give input, the script will detect a certain string of letters that are inappropriate.

For example, this would be useful for asking a player what they would like to name their character (like in a text/mud game). Here's what I have so far and it works well enough:
------------------
print("Please tell me the name of the character you wish to create.\"")
confirmed = False
while not confirmed:
name = input()
while name == '' or not name.isalpha() or len(name) >=25 or len(name) <3:
print("\tSorry, you must pick a more appropriate name.")
print("Your name must not be blank, must be alphabetical only (no numbers)...")
print("and must be between 3 and 25 characters.")
print('')
print("Please tell me the name of the character you wish to create.")
name = input()
-------------------

So I'd also like to include in the code whether the input() includes banned words/syllables like 'ass' or 'shit' etc, and reject the name. Any help would be greatly appreciated!

print("A sage comes up to you.  He says, \"Greetings, Adventurer!  Please tell me the name of the character you wish to create.\"")
confirmed = False
while not confirmed:
    name = input()
    while name == '' or not name.isalpha() or len(name) >=25 or len(name) <3:
        print("\tSorry, you must pick a more appropriate name.")
        print("Bear in mind, your name must not be blank, must be alphabetical only (no numbers)...")
        print("and must be between 3 and 25 characters.")
        print('')
        print("Please tell me the name of the character you wish to create.")
        name = input()
Reply
#2
inappropriate = "<>#!@$%^&*()_- {}"
if not any(map(lambda char: char in inappropriate, name)):
    # The name is not alright.

the map function is mapping the lambda function with any character in the name.  This will produce a list of the results of the lambda function. And any() returns True if there is not any value in the list which could be considered False - None, 0, False, empty value.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
@wavic
using if not any is wrong and would yield the opposite result
name = 'test'
inappropriate = "<>#!@$%^&*()_- {}"
if not any(map(lambda char: char in inappropriate, name)):
    print('invalid')
Output:
invalid
and there is no need to complicate it with using lambda, keep it simple

inappropriate = "<>#!@$%^&*()_- {}"
if any(char in inappropriate for char in name):
    # The name is not alright.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  difference between forms of input a list to function akbarza 6 1,022 Feb-21-2024, 08:02 PM
Last Post: bterwijn
  Basic Coding Question: Exit Program Command? RockBlok 3 554 Nov-19-2023, 06:31 PM
Last Post: deanhystad
  Question on dir() function Soorya25 1 1,141 Jan-16-2023, 09:33 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,080 Dec-25-2022, 03:00 PM
Last Post: askfriends
  Function not scriptable: Noob question kaega2 3 1,174 Aug-21-2022, 04:37 PM
Last Post: kaega2
  Showing an empty chart, then input data via function kgall89 0 975 Jun-02-2022, 01:53 AM
Last Post: kgall89
  [solved] Basic question on list matchiing paul18fr 7 1,857 May-02-2022, 01:03 PM
Last Post: DeaD_EyE
  input function question barryjo 12 2,706 Jan-18-2022, 12:11 AM
Last Post: barryjo
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,111 Jan-15-2022, 06:07 PM
Last Post: deanhystad
Big Grin General programming question (input string)[ jamie_01 2 1,592 Jan-08-2022, 12:59 AM
Last Post: BashBedlam

Forum Jump:

User Panel Messages

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