Python Using Lists to create program - python

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.

Related

Checking if input matches variable with a list of strings

I am trying to make a calculator and the user must input which operation they want to use, I have only done addition so far but you get the point.
welcome = str(input("Welcome to the calculator, which form of operation do you wish to choose? "))
Addition = "Add", "add", "Addition", "addition", "+"
for x in Addition:
if welcome == Addition:
print("Works")
I run the code and when I input any of the strings in the "Addition" variable, it does not print "Works"?.
I have tried without the for loop and it still does not print "Works"
I am an absolute beginner at python and I just made this stackoverflow account today. I don't know how to write the code in proper code form in this question.
You are probably looking for in we can also use .lower() on the input so we have fewer things to match.
welcome = str(input("Welcome to the calculator, which form of operation do you wish to choose? ")).lower()
addition = ['add','addition','+']
if welcome in addition:
print('worked')

Presence check on python?

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()

How do I find a word in a txt file using an input variable in python?

key=input("What is your word? ")
ans=open("file.txt","r")
for line in ans:
for word in line.split():
if (key) in (word):
print("Yes")
The code works when I write it without the input variable but as soon as I do it comes back saying that the text I entered is not defined.
Change input to raw_input and the code will work as expected. input evaluates whatever user enters as Python code where as raw_input returns the user input as a string.
In Python2, input() is not the same as in Python3. In Python2, input() is the same as eval(raw_input()). That is, it converts what the user types into Python code. When the user types a normal word, Python tries to evaluate it but finds that the variable is not defined. In Python2, you need to use raw_input(), not input().

i made dictionary, when i am typing word it says the word donot exist in dictionary,why?

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.

Python - my while loop is stopping itself

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.

Categories