Python学习笔记:If、While

1.if

if的基本结构:

if xxxxx:
    xxxxxxx
elif xxxx:
    xxxxxxx
else:
    xxxxxxx

通过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")

通过多个条件判断: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

while的基本结构:

while xxx:
    xxxxx

猜数字游戏:通过random中的randint生成确定范围内的随机整数,通过不断输入guess实现和目标答案的大小判断并不断缩小答案区间;

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

汽车前进游戏:输入对应操作实现汽车的相应动作

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

你可能感兴趣的:(Python,学习,笔记)