Hi im new with python but in my school i was asked to write a code that ask the user to input a word and a sentence with few feature but it must have a presence check (so the user cant just leave the question blank or the word) but i dont know how to put a presence check, also if the word or sentence is left blank the user should be able to re-enter it again. can anyone help me?
Use a while loop as long as UserSen is empty:
UserSen = input("Please type in a sentence without punctuation: ").lower()
while len(UserSen) == 0:
UserSen = input("Please type in a sentence without punctuation: ").lower()
UserSen = UserSen.split()
Related
I am helping a friend with some code for a school helping bot, but I've run into a problem. On the last line, where it says: if helpselect == ('*****'): , I am trying to add multiple conditions in one line of code so that the specific line of code activates when they type math or math. HELP
import time
print('Hello, my name is CareBot and I am here to help!')
time.sleep(3)
name = input('Please input your name: ')
print('Nice to meet you, %s. I am a bot that can lead you to resources to help out with school subjects!' % name)
time.sleep(2)
print('Now, what subject can I help you with?')
helpselect = input('Please input a subject: ')
if helpselect == ('*****'):
if helpselect in {"math", "english"}:
print("You selected math or english")
Something like that? (You said "when they type math or math", which is redundant.)
If you're just trying to check if the person typed a certain subject then I might create a set of strings. When you check if something is in a set of strings you are checking whether or not the word you typed is one of the words in the set of strings.
setofstrings = ["math", "english", "science"]
if helpselect in setofstrings:
or perhaps you want to do this.
if helpselect == "math" or helpselect == "english":
If you're trying to check if there are any math symbols in a line of code.
What I would do is create a string called mathsymbols.
mathsymbols = "+-*/"
Then check and see if the input contains any math symbols by doing this line of code. This checks if what you typed any individual character in the string mathsymbols.
if helpselect in mathsymbols:
I would want learn sets, strings, the "in" operator, "and", "or", etc.
In this program i am making a dictionary.
When i run this program and enter 1 in the menu the program asks me to search meaning, but when i type the word 404(which is in the dictionary) it says Word donot exist in dictionary. Where does this problem come from?
print("This is a dictioinary")
print("\t\t\tWelcome to Geek Translator Game")
dictionary={"404":"Message not found","googler":"person who seaches on google"}
choose=None
while(choose!=0):
choose=input('''0.Exit
1.search meaning
2.Add Word
3.Replace meaning''')
if choose is 0:
print("bye")
if choose is 1:
word=input("Enter the word\n")
if word in dictionary:
meaning=dictionary[word]
print(meaning)
else:
print("Word donot exist in dictionary")
if choose is 2:
word=input("Enter the word\n")
if word in dictionary:
print("Word already exists in dictionary")
print("Try replacing meaning by going in option 3")
else:
defination=input("Enter the defination")
dictionary[word]=defination
if choose is 3:
word=input("Enter the term\n")
if word in dictinary:
meaning=input("Enter the meaning\n")
dictionary[word]=meaning
else:
print("This term donot exist in dictionary")
input() interprets user input as a Python expression. If you enter 404, Python interprets that as an integer. Your dictionary, however, contains a string.
You'll have to enter "404" with quotes for this to work correctly. Your better option would be to use raw_input() instead, to get the raw input the user typed without it having to be formatted as a Python expression:
word = raw_input("Enter the word\n")
Do this everywhere you use input() now. For your user menu input, you should really use int(raw_input("""menu text""")) rather than input(). You might be interested in Asking the user for input until they give a valid response to learn more about how to ask a user for specific input.
Next, you are using is to test user choices. That this works at all is a coincidence, as Python has interned small integers you are indeed getting the same 0 object over and over again and the is test works. For almost all comparisons you want to use == instead however:
if choose == 0:
# ...
elif choose == 1:
# etc.
I have an assignment in class to write a program using lists in Python
"Create a program that prompts the user for a vocabulary word. Then prompts user to enter the word's definition. Ask user if they want to enter more words and definitions. When they are done entering all words and definitions, print out all the words along with their definition."
I know I need to have a nested list to store the user input. But my question is how am I going to get the user input and store it into a nested list? I also know that I need to use a loop to take in all the inputs for words and definitions, but I'm confused on how to do so.
myvar=str(print(input("Type a Word.")))
myvar2=str(print(input("Type the word's definition.")))
myvar3=input(print("If you want to enter another word, enter Y, if not enter N"))
mylist=[[myvar,myvar2]]
while myvar3=='Y':
myvar4=str(print(input("Enter your next word.")))
myvar5=str(print(input("Enter the word's definition.")))
mylist.append([myvar4,myvar5])
myvar3=input(print("If you want to enter another word, enter Y, if not enter N"))
print(mylist)
I think this works, is there anything wrong with this? Do I need to make it to where if they enter "N" it does something to end the loop? Or does the loop just end as long as it doesn't equal 'Y'?
If you're using Python 3.x, getting input from a user is very simple. This is accomplished using the input() function.
This will prompt input from the user, printing the string passed to input() before the caret:
input("Please enter a word: ")
The user types whatever they feel, then hits Enter. When they hit enter, input() returns the text they've entered. So, you can store the value the user typed with something like this:
user_word = input("Please enter a word: ")
And a definition can be entered into a separate variable like this:
user_definition = input("Please enter a definition: ")
Then, you can use one of Python's built-in data types to store both values, and, just as importantly, to build a logical association between them, before you prompt them for their next word.
Here's the documentation on the input and output.
This is a homework question and it asks "Pick a word. Ask the user to enter a letter. Check how many times the letter is present in your word and output the number on the screen." So far I have written it as is and it seems to be working fine:
word = str("python")
letters = len(word)
attempt = str(raw_input ("Enter a letter: "))
while attempt in word:
count = "python".count(attempt)
if attempt in word:
print "This letter is present ",count, "time(s)."
attempt = str(raw_input ("Enter another letter: "))
while attempt not in word:
attempt = str(raw_input ("This letter is not present, enter another letter: "))
count = "python".count(attempt)
if attempt in word:
print "This letter is present ",count, "time(s)."
but occasionally if I am inputting letters the program will stop and no longer take any more inputs. What am I doing wrong? I apologize if the code is very sloppy and poorly written as I am new to programming. Thanks.
I'm not sure if this is what you're getting at, but your while loops will execute one after the other. They don't both apply all the time. So the first while means "keep doing this as long as the user enters a letter that is in the word", and that loop will keep executing as long as the user enters letters in the word. As soon as the user enters one letter that isn't in the word, the loop will end and things will move on to the second loop. Execution will never go back to the first loop. Likewise, once in the second loop, if you then enter a letter that is in the word, the loop will end. Since that's the end of the program, the program will end.
So if you enter a letter that isn't in the word, then enter a letter that is in the word, the program will end. Like if you first enter "x" and then enter "y", it will then stop.
I think what you really want is more like:
while True:
attempt = raw_input("Enter a letter:")
if attempt in word:
print "That was in the word", word.count(attempt), "times"
else:
print "That was not in the word"
Of course, this program will loop infinitely until you close it by pressing Ctrl-Break or the like.
There are some other issues with your code. You don't need to wrap "python" in str, since that already is a string. You also don't need to wrap raw_input in a string, since raw_input already returns a string. You define a variable called letters but never use it.
Also, you define word = "python" at the beginning, but then sometimes later you use the word variable, while other times you retype the string "python" in your code. It doesn't matter for this program, but in general it's a good idea to assing the variable once and use that everywhere; otherwise you'll have to change it in many places if you decide to use a different word, which increases the likelihood that you'll forget to change it in one place, and thereby cause a bug.
Finally, note that in and count operate on substrings, not just single letters. So entering "yth" as as your input will still work and give a count of 1. There's not necessarily anything wrong with this, but you should be aware that although you're asking for letters the person can type in anything, and substrings of any length will still be found in the "word".
The program is sequential with two loops. Once those loops have been passed the program ends.
Loop 1: run while the input is found in the word.
As soon as this condition fails, we fall through to loop 2:
Loop 2: run while the input is not found in the word.
As soon as this condition fails, we fall through to the end of the program.
So, if you enter a "bad" input once, then a "good" input once, the program will end.
What you want to do is wrap both loops into one, and use an if-else to decide which each particular input is.
I have to write a program in python where the user is given a menu with four different "word games". There is a file called dictionary.txt and one of the games requires the user to input a) the number of letters in a word and b) a letter to exclude from the words being searched in the dictionary (dictionary.txt has the whole dictionary). Then the program prints the words that follow the user's requirements. My question is how on earth do I open the file and search for words with a certain length in that file. I only have a basic code which only asks the user for inputs. I'm am very new at this please help :(
this is what I have up to the first option. The others are fine and I know how to break the loop but this specific one is really giving me trouble. I have tried everything and I just keep getting errors. Honestly, I only took this class because someone said it would be fun. It is, but recently I've really been falling behind and I have no idea what to do now. This is an intro level course so please be nice I've never done this before until now :(
print
print "Choose Which Game You Want to Play"
print "a) Find words with only one vowel and excluding a specific letter."
print "b) Find words containing all but one of a set of letters."
print "c) Find words containing a specific character string."
print "d) Find words containing state abbreviations."
print "e) Find US state capitals that start with months."
print "q) Quit."
print
choice = raw_input("Enter a choice: ")
choice = choice.lower()
print choice
while choice != "q":
if choice == "a":
#wordlen = word length user is looking for.s
wordlen = raw_input("Please enter the word length you are looking for: ")
wordlen = int(wordlen)
print wordlen
#letterex = letter user wishes to exclude.
letterex = raw_input("Please enter the letter you'd like to exclude: ")
letterex = letterex.lower()
print letterex
Here's what you'd want to do, algorithmically:
Open up your file
Read it line by line, and on each line (assuming each line has one and only one word), check if that word is a) of appropriate length and b) does not contain the excluded character
What sort of control flow would this suggest you use? Think about it.
I'm not sure if you're confused about how to approach this from a problem-solving standpoint or a Python standpoint, but if you're not sure how to do this specifically in Python, here are some helpful links:
The Input and Output section of the official Python tutorial
The len() function, which can be used to get the length of a string, list, set, etc.
To open the file, use open(). You should also read the Python tutorial sec. 7, file input/output.
Open a file and get each line
Assuming your dictionary.txt has each word on a separate line:
opened_file = open('dictionary.txt')
for line in opened_file:
print(line) # Put your code here to run it for each word in the dictionary
Word length:
You can check the length of a string using its str.len() method. See the Python documentation on string methods.
"Bacon, eggs and spam".len() # returns '20' for 20 characters long
Check if a letter is in a word:
Use str.find(), again from the Python sring methods.
Further comments after seeing your code sample:
If you want to print a multi-line prompt, use the heredoc syntax (triple quotes) instead of repeated print() statements.
What happens if, when asked "how many letters long", your user enters bacon sandwich instead of a number? (Your assignment may not specify that you should gracefully handle incorrect user input, but it never hurts to think about it.)
My question is how on earth do I open the file
Use the with statement
with open('dictionary.txt','r') as f:
for line in f:
print line
and search for words with a certain length in that file.
First, decide what is the length of the word you want to search.
Then, read each line of the file that has the words.
Check each word for its length.
If it matches the length you are looking for, add it to a list.