i am new to python and i'm just trying simple exercises with if statements but i want my statement to loop, what i have is...
species = "cat"
if species == "cat":
print("yep, it's a cat")
species = "dog"
if species == "dog":
print("and that's a dog")
species = "horse"
if species == "horse":
print("look at that horse over there")
species = "cheetah"
if species == "cheetah":
print("WHERE DID THE CHEETAH COME FROM?")
(yes it is a little silly) is there a way to continuously loop the statement and is there a way to limit the amount of times it loops? if there is a better way do what i am trying to do and if what what is it?
I would store this data in a dictionary, and then you can loop through that:
responses = {
"cat": "yep, it's a cat",
"dog": "and that's a dog",
"horse": "look at that horse over there",
"cheetah": "WHERE DID THE CHEETAH COME FROM?"
}
for species in ['cat', 'dog', 'horse', 'cheetah']:
response = responses.get(species, 'none of those here')
print(response)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.