+ 2
any() function in python
Can you please have a look what I’m doing wrong? I want the input() to be exactly the same either one of the hogwarts houses to proceed to the next step four = ["gryffindor", "slytherin", "hupplepuff", "ravenclaw"] school = any(four) house = "" house_count = 0 house_limit = 3 out_of_tries = False while (house != school) and not(out_of_tries): if house_count < house_limit: house = input("House that you belong to? house_count = house_count + 1 else: out_of_tries = True TY
8 Respuestas
+ 8
https://code.sololearn.com/cbs8C2LJQWCr/?ref=app
The any() function is True when there is at least one truthy value in the collection. In your case, school is set to True in the second line, because all elements of the list are nonempty strings, having a truthy value.
If you want to select a random value from the list, you can do this:
import random
school = random.choice(four)
+ 6
I used the ‘in’ function and it works 😅
+ 4
Bea Ramillano good idea. I should have read your question more carefully, I was stuck at "any" and wondered why you would use it in that place. :)
+ 2
This is what I came up with :) feel free to give me feedback or things to improve
https://code.sololearn.com/cvasqFhQX7mF/?ref=app
+ 1
Tibor Santa thanks for the help :) the any() function came up first so I thought it was the best function for this scenario. I am still a beginner and have a lot to learn :)
+ 1
Your code is good as it is for now. I'm glad you figured this out on your own, great job :)
Unfortunately Sololearn is not so great at running interactive code like this, with multiple inputs, but I get the gist.
And I am also a big fan of Harry Potter :)
+ 1
Tibor Santa thanks for the feedback fellow potterhead :) greatly appreciated! Yea I use a different app to write the code since I cannot add an input in Sololearn. I almost spent the whole night trying to make this code work but definitely worth it once you see it all work as you planned and definitely a good practice since I learnt a new function along the way :)
0
import time
from colorsys import hsv_to_rgb
def interpolate_color(start_color, end_color, steps):
"""
Interpolate between two colors in RGB space.
"""
r1, g1, b1 = start_color
r2, g2, b2 = end_color
dr = (r2 - r1) / steps
dg = (g2 - g1) / steps
db = (b2 - b1) / steps
for i in range(steps):
r = int(r1 + i * dr)
g = int(g1 + i * dg)
b = int(b1 + i * db)
yield (r, g, b)
def sky_color(hour):
"""
Calculate the color of the sky based on the hour of the day.
"""
if hour < 6:
return (50, 50, 50) # dark grey
elif hour < 12:
return (0, 191, 255) # light blue
elif hour < 18:
return (135, 206, 250) # sky blue
else:
return (25, 25, 112) # dark blue
def sun_color(hour):
"""
Calculate the color of the sun based on the hour of the day.
"""
if hour < 6:
return (255, 255, 255) # white
elif hour < 12:
return (255, 215, 0) # gold
elif hour < 18:
return