stephenblack
stephenblack

Reputation: 31

How do I keep asking the user to input the valid characters?

I am writing a program called hangman.py. In my program the user cannot input "?' or whitespace in my input. For example, the user cannot enter: '?xx?xx?' or 'How do I do this'. But the user can input something like 'ldkdjgg' or 'stop-go'. If the user enter something like '?xxxxx?' or 'How do I do this' I have to keep asking the user "Please enter a word to be guessed that does not contain ? or white space:". My question is how do I print "Please enter a word to be guessed that does not contain ? or white space:" until the user stop entering '?' or whitespace in the input.

This is my idea but I am having trouble printing out "Please enter a word to be guessed that does not contain ? or white space:" if i enter a '?' or whitespace in my input

print("Please enter a word to be guessedthat does not contain ? or white space: ",end='')
while True:
  try:
      secret_word=input()
  except '?' or 'print()'
      print("Please enter a word to be guessedthat does not contain ? or white space: ",end='')
      continue
  else:
      break

Upvotes: 0

Views: 303

Answers (4)

Stefan Pochmann
Stefan Pochmann

Reputation: 28596

secret_word = " "
while set(secret_word) & set("? "):
    secret_word = input("Please enter a word to be guessedthat does not contain ? or white space: ")

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180401

You can check if set("? ") intersects with secret_word, if it does the user entered at least one space or ?:

while True:
    secret_word = input("Please enter a word to be guessedthat does not contain ? or white space: ")
    if not set("? ").intersection(secret_word):
        break
    else:
        print("Invalid input")

Demo:

Please enter a word to be guessedthat does not contain ? or white space: ?
Invalid input
Please enter a word to be guessedthat does not contain ? or white space: foo bar
Invalid input
Please enter a word to be guessedthat does not contain ? or white space: foo

Or use str.translate and compare the lengths after removing any spaces and ?:

tbl = str.maketrans({ord("?"):"",ord(" "):""})
while True:
    secret_word = input("Please enter a word to be guessedthat does not contain ? or white space: ")
    if len(secret_word.translate(tbl)) == len(secret_word):
        break
    else:
        print("Invalid input")

secret_word.translate(tbl) will remove any spaces or ? so if the length is the same after translating we know the user did not enter any, if it is different then we know they have. You can add as many bad characters as you want to the tbl dict using the ord as the key and an empty string as the value.

Upvotes: 0

user4205830
user4205830

Reputation:

A slightly more readable solution:

user_input = raw_input("Please enter a string that doesn't contain spaces or '?':")

while " " in input or "?" in input:
    user_input = raw_input("Please enter a string that doesn't contain spaces or '?':")

Upvotes: 2

101
101

Reputation: 8989

Something like this should work:

bad_strings = [' ', '?']
secret_word = ' '

while any([s in secret_word for s in bad_strings]):
    secret_word = input("Enter a word that doesn't contain '?' or spaces: ")

Upvotes: 0

Related Questions