Python study notes: If, While

1.if

The basic structure of if:

if xxxxx:
    xxxxxxx
elif xxxx:
    xxxxxxx
else:
    xxxxxxx

Examples judged by boolean

is_hot = True
is_cold = True
if is_hot:
    print("it's a hot day\nDrink plenty of water")
elif is_cold:
    print("it's a cold day\nWear warm clothes")
else:
    print("it's a lovely day")

Judge by multiple conditions: and, or

price = 567.8
is_good_credit = True
is_high_income = True
answer = input("Do you have good credit?\n").lower()
if answer == 'yes':
    is_good_credit = True
else:
    is_good_credit = False
answer1 = input("Do you have high income?\n").lower()
if answer1 == 'yes':
    is_high_income = True
else:
    is_high_income = False
if is_good_credit and is_high_income:
    print(f"You need to pay ${price * 0.8}")
elif is_good_credit or is_high_income:
    print(f"You need to pay ${price * 0.9}")
else:
    print(f"You need to pay ${price*1}")
Do you have good credit?
yes
Do you have high income?
yes
You need to pay $454.24

Process finished with exit code 0

2.while

The basic structure of while:

while xxx:
    xxxxx

Number guessing game: Generate random integers within a certain range through randint in random, and continuously input guess to realize and judge the size of the target answer and continuously reduce the answer range;

import random
i = 1
i_max = 10  # 限定参与次数
ans = random.randint(1, 100)     # 随机生成正确答案
ans_min = 1
ans_max = 100
print("the answer is in [1, 100]")
while i <= i_max:
    i += 1
    guess = int(input("Pleas input your answer here:"))
    if guess == ans:
        print(f"Congratulations! {guess} is the correct answer!")
        break
    elif guess > ans:
        ans_max = guess
        print(f"Sorry! Your answer {guess} is bigger than correct answer! The answer is in [{ans_min}, {ans_max}]")
    else:
        ans_min = guess
        print(f"Sorry! Your answer {guess} is smaller than correct answer! The answer is in [{ans_min}, {ans_max}]")
the answer is in [1, 100]
Pleas input your answer here:50
Sorry! Your answer 50 is bigger than correct answer! The answer is in [1, 50]
Pleas input your answer here:16
Sorry! Your answer 16 is smaller than correct answer! The answer is in [16, 50]
Pleas input your answer here:28
Sorry! Your answer 28 is bigger than correct answer! The answer is in [16, 28]
Pleas input your answer here:20
Sorry! Your answer 20 is smaller than correct answer! The answer is in [20, 28]
Pleas input your answer here:22
Sorry! Your answer 22 is smaller than correct answer! The answer is in [22, 28]
Pleas input your answer here:26
Sorry! Your answer 26 is smaller than correct answer! The answer is in [26, 28]
Pleas input your answer here:27
Congratulations! 27 is the correct answer!

Process finished with exit code 0

Car forward game: input corresponding operations to realize the corresponding actions of the car

quit_flag = False
while not quit_flag:
    msg = input("Please input :").lower()
    if msg == "help":
        print("start: Run the car \n stop: Stop the car \n quit: Quit the game \n help: Get message")
    elif msg == "start":
        print("The car started")
    elif msg == "stop":
        print("The car stopped")
    elif msg == "quit":
        quit_flag = True
    else:
        print("Wrong action! Please try again!")
Please input :help
start: Run the car 
 stop: Stop the car 
 quit: Quit the game 
 help: Get message
Please input :start
The car started
Please input :stop
The car stopped
Please input :asbdkagsf
Wrong action! Please try again!
Please input :quit'
Wrong action! Please try again!
Please input :quit

Process finished with exit code 0

Guess you like

Origin blog.csdn.net/YGZ11113/article/details/132118028