1.wenn
Die Grundstruktur von if:
if xxxxx:
xxxxxxx
elif xxxx:
xxxxxxx
else:
xxxxxxx
Nach booleschen Werten bewertete Beispiele
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")
Beurteilen Sie anhand mehrerer Bedingungen: und, oder
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.während
Die Grundstruktur von while:
while xxx:
xxxxx
Zahlenschätzspiel: Generieren Sie zufällige Ganzzahlen innerhalb eines bestimmten Bereichs durch Randint in Random und geben Sie kontinuierlich Schätzungen ein, um die Größe der Zielantwort zu erkennen und zu beurteilen und den Antwortbereich kontinuierlich zu reduzieren.
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
Auto-Vorwärtsspiel: Geben Sie entsprechende Operationen ein, um die entsprechenden Aktionen des Autos zu realisieren
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