Python random.choice()

Asked 1 years ago, Updated 1 years ago, 60 views

import random time = random.randint(1,24) print ("Good morning". This time is "+ str(time) + ".") sunny = random.choice([True,False]) if sunny: print ("It's sunny now."") else: print ("It's not sunny at the moment") if time >=6 and time < 9 and sunny: print ("The lark sings"") else: print("Larks Don't Sing")

The source code is as above. The question I want to ask is sunny = random.choice([True,False]) if sunny: print ("It's sunny now."") else: print ("It's not sunny at the moment") It's this part. Why is Sunny == True: not Sunny:?

random

2022-09-21 20:57

1 Answers

It's the same result. Even if it's just if sunny: it's absolutely true if Sunny isn't an empty value (empty string, bin list, bin tuple, bin dictionary), 0, None, False. Of course, direct sunny==compared with a comparison statement of any value, the two values are true if they are equal, and false if they are different. If the two cases are the same result, there is no need to create duplicate codes using equivalent operators.


2022-09-21 20:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.