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
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mygreatlibrary-prebuilt
MY_LIBRARY_NAME := myGreatLibrary
### export include path
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
### path to library
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/lib$(MY_LIBRARY_NAME).so
### export dependency on the library
LOCAL_EXPORT_LDLIBS := -L$(LOCAL_PATH)/libs/$(TARGET_ARCH_ABI)/
LOCAL_EXPORT_LDLIBS += -l$(MY_LIBRARY_NAME)
include $(PREBUILT_SHARED_LIBRARY)
答案 0 :(得分:2)
稍微更易读的解决方案:
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 '?':")
答案 1 :(得分:0)
You can check if :h 'windheight'
:h 'winminheight'
intersects with set("? ")
, if it does the user entered at least one space or ?:
secret_word
Demo:
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")
Or use str.translate and compare the lengths after removing any spaces and ?:
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
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")
will remove any spaces or secret_word.translate(tbl)
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 ?
dict using the tbl
as the key and an empty string as the value.
答案 2 :(得分:0)
这样的事情应该有效:
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: ")
答案 3 :(得分:0)
secret_word = " "
while set(secret_word) & set("? "):
secret_word = input("Please enter a word to be guessedthat does not contain ? or white space: ")