Multiple strings in one variable

2024/10/9 0:39:53

Say, for example, I have some code that goes like this:

sentence = input("Enter input: ")
target_letter = "a" or "b" or "c"
print(sentence.index(target_letter))

My main aim is to get the program to print any of the target letters' minimum index in the sentence. However when the Boolean "or" operators are used here it only recognizes the first string in the variable. For example: If I type

Ice cream

The program should recognize the 'c' as the first valid string in the index and print "1" but it skips to 'a' and prints "7".

The same sort of problem arises with the "and" operator:

sentence = input("Enter input: ")
target_letter = "a" and "b" and "c"
print(sentence.index(target_letter))

This time the program only recognizes the last string in the variable instead, if I type:

Exactly

The program throws back "3" becuase c's index is 3 but the first letter is actually 'a' with index 2. And when the input had only a "b" in it out of the strings, the program threw an error because it was neither the first string or the last string.

How would this problem be fixed so that I could place multiple possible strings in a variable so that one of the strings in the variable would be recognized later in the program?

Answer

Let's examine the target_letter = "a" or "b" or "c" first.

>>> target_letter = "a" or "b" or "c"
>>> target_letter  # will always return 'a' because 'a' is true. 
'a'
>>> #  The only string which will return false is the empty string '' or "".

A proposed solution

sentence = input("Enter input: ")
target_letters = ["a", "b", "c"]
for letter in sentence:if letter in target_letters:print(letter, 'was found at location in ',sentence.index(letter))else:print(letter, "was not found in sentence,", sentence)

Output

Enter input: Ice creeam
I was not found in sentence, Ice creeam
c was found at location in  1
e was not found in sentence, Ice creeamwas not found in sentence, Ice creeam
c was found at location in  1
r was not found in sentence, Ice creeam
e was not found in sentence, Ice creeam
e was not found in sentence, Ice creeam
a was found at location in  8
m was not found in sentence, Ice creeamProcess finished with exit code 0
https://en.xdnf.cn/q/118651.html

Related Q&A

How to get the area of shape filled using turtle

I graphed a fractal shape in Python using turtle, and am trying to get the area of this fractal after a sufficiently high iteration. This fractal is related to the Koch snowflake, for those interested.…

What distinguishes a command from needing () vs not?

I recently spent way too long debugging a piece of code, only to realize that the issue was I did not include a () after a command. What is the logic behind which commands require a () and which do not…

python iterate yaml and filter result

I have this yaml file data:- name: acme_aws1source: awspath: acme/acme_aws1.zip- name: acme_gke1source: gkepath: acme/acme_gke1.zip- name: acme_ocisource: ocipath: acme/acme_oci1.zip- name: acme_aws2so…

Faster way to looping pixel by pixel to calculate entropy in an image

I have been calculating the entropy of an image with a pixel by pixel convolution operation, and it has been working but very slowly, increasing the execution time with the kernel size. Here is my func…

Google AppEngine - updating my webapp after deploy

friends! Im fairly new to web app world and I have a question regarding Google AppEngine functions. Ive installed the Launcher on my machine and signed up for the online platform (Python). Ive added …

Extract GPS coordinates from .docx file with python

I have some hectic task to do for which I need some help from python. Please see this word document.I am to extract texts and GPS coordinates from each row. There are currently over 100 coordinates in …

How to Serialize SQL data after a Join using Marshmallow? (flask extension)

I have 2 tables in SQL: class Zoo(db.Model):id = db.Column(db.Integer, primary_key=True)nome = db.Column(db.String(80), unique=True, nullable=False)idade = db.Column(db.Integer, unique=False, nullable=…

Python readline() is not reading a line with single space

I am reading a text file using readline(). My file contains below content with a space at second line:!" # $ % & When I print read values using-print("%s\n" % iso5_char)It prints-!&q…

Bisection search [duplicate]

This question already has answers here:Closed 11 years ago.Possible Duplicate:Using bisection search to determine I have posted other thread but it did not receive answers thus im trying to provide so…

What am i doing wrong with matplotlibs yticks?

My code is as follows (dont ask for the variable names, im german^^):import matplotlib.pyplot as plt import numpy as np strecke = [] zeit = []daten = open("BewegungBeschleunigung.csv")for i i…