Checking if input matches variable with a list of strings - python

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

Related

Python printing beginner

I just started with python. My teacher gave me an assignment and I'm stuck on a project where I have to make the numbers of characters appear when someone enters their name for input command input("what is your name") I don't think I have been taught this and google is giving me a hard time when trying to look for the command. This might be Childs play to most but can anyone throw me a tip/hint?
using print(len(myVariable)) should output the number of characteres that the string has. You should familiarize yourself with python methods.
Here are some resources:
https://docs.python.org/3/library/functions.html
https://www.w3schools.com/python/ref_func_len.asp
Printing the length of a variable is very easy with Python due to the many built-in functions you can perform on strings! In this case, you would use use len(). Read about other helpful string methods in python too in order to get a head start on your class!
inputtedName = input("What is your name? ")
nameLength = len(inputtedName)
print("Your name is " + str(nameLength) + " letters.")
print(f"Your name is {nameLength} letters.")
The first print statement uses something called string concatenation to create a readable sentence using variables. The second uses f strings to make using variables in your strings even easier!

How do I stop an input when i entered a certain word?

I´m doing a small work for college and i need to stop a certain input when u entered a certain word in python?
I tried this but it isn´t working
if (entrance == 'STOP'):
break;
FIrst of all always provide the maximum information you can about your question. Secondly the brackets around the if conditional and the semi column are redundant in python.
For your problem, try using while loops to test the input of the user.
Something like,
while input() != 'STOP':
# insert code here

Input a message inside a box

I need to create a box with parameters that prints any input the user puts in. I figured that the box should be the length of the string, but I'm stuck with empty code, because I don't know where to start.
It should look like this:
I agree with Daniel Goldfarb comments. Don't look for help without trying.
If you still couldn't get how to do that, then only read my remaining comment.
Just print :
str = string entered
len(str) = string length
+-(len(str) * '-')-+
| str |
+-(len(str) * '-')-+
So hopefully you can learn, don't want to just write the code for you. Basically break it into steps. First you need to accept user input. If you don't know how to do that, try googling, "python accept user input from stdin" or here is one of the results from that search: https://www.pythonforbeginners.com/basics/getting-user-input-from-the-keyboard
Then, as you mentioned, you need the length of the string that was input. You can get that with the len function. Then do the math: It looks like you want "|" and two spaces on each side of the string, giving the length plus 6 ("| " on either side). This new length is what you should make the "+---+" strings. Use the print() function to print out each line. I really don't want to say much more than that because you should exercise your brain to figure it out. If you have a question on how to generate "+---+" of the appropriate length (appropriate number of "-" characters) you can use string concatenation and a loop, or just use the python string constructor (hint: google "construct python string of len repeat characters"). HTH.
One more thing, after looking at your code, in addition to my comment about printing the string itself within the box, I see some minor logic errors in your code (for example, why are you subtracting 2 from the width). THE POINT i want to me here is, if you ware going to break this into multiple small functions (a bit overkill here, but definitely a good idea if you are just learning as it teaches you an important skill) then YOU SHOULD TEST EACH FUNCTION individually to make sure it does what you think and expect it to do. I think you will see your logic errors that way.
Here is the solution, but I recommend to try it out by yourself, breakdown the problem into smaller pieces and start from there.
def format(word):
#It declares all the necessary variables
borders =[]
result = []
# First part of the result--> it gives the two spaces and the "wall"
result.append("| ")
# Second part of the result (the word)
for letter in word:
result.append(letter)
# Third part of the result--> Ends the format
result.append(" |")
#Transforms the list to a string
result = "".join(result)
borders.append("+")
borders.append("--"+"-"*len(word)+"--")
borders.append("+")
borders="".join(borders)
print(borders)
print(result)
print(borders)
sentence = input("Enter a word: ")
format(sentence)
I'm new to Python, and I've found this solution. Maybe is not the best solution, but it works!
test = input()
print("+-", end='')
for i in test:
print("-", end='')
print("-+")
print("| " + test + " |")
print("+-", end='')
for i in test:
print("-", end='')
print("-+")

Loop function yet break if certain code put into raw_input

I've been looking up on here and Google yet can't find the solution for what I'm looking for. "While" function keeps failing me as I don't know here I need to put it. I'm a total newbie so if you could kindly explain the solution, thank you:
number = raw_input("Number: ")
if int(number) % 2 == 0:
print "Even number"
else:
print "Odd number"
All I would like to do is to keep the function looping allowing the user to enter as many numbers as possible. It should only break if user puts in "stop" as the value.
Well if user types stop, program stops.So we need to check for that when we create our while loop like:
number_from_user = raw_input("Number: ")
while number_from_user != "stop":
try:
if int(number_from_user)% 2 == 0:
print "Even number"
else:
print "Odd number"
number_from_user = raw_input("Number:")
except ValueError:
print("Enter a number please")
number_from_user = raw_input("Number:")
And I suggest you make yourself familiar with while loops, which I found a video explaining and making an example "Guess the number game" using python. It should help you enough to make you able to solve your own problem with while loops.
https://www.youtube.com/watch?v=tu0zlBFRa_c
or:
https://www.youtube.com/watch?v=PoPxclbw6uc
And I suppose you're using python 2.7, in those videos they're using python 3+, you should type raw_input where they're typing input.
Edited: Added try and except.

Python Using Lists to create program

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.

Categories