if判断学习 (1)体重转换程序 weight=int(input('Weight:')) unit=input('(L)bs or (K)g:') if unit == "L": converted = weight * 0.45 print(f"You are {converted} killos") else: print(f"You are {converted} pounds") converted= weight // 0.45
(2)猜谜游戏 secret_number = 9 guess_count = 0 guess_limit = 3 while guess_count < guess_limit: guess = int(input('Guess:')) guess_count += 1 if guess == secret_number: print('You win') break else: print('Sorry,you failed!')
(3)赛车游戏 command = "" started = False #while command != "quit": while True: command = input(">").lower() if command == "start": if started: print("Car is already started!") else: started = True print("Car started...") elif command == "stop": if started: started = False print("Car stopped.") else: print("Car is already stoped!") elif command == "help": print(''' start - to start the car stop - to stop the car quit - to quit ''') elif command == "quit": break else: print("I don't understand that!")
command = "" started = False #while command != "quit": while True: command = input(">").lower() if command == "start": if started: print("Car is already started!") else: started = True print("Car started...") elif command == "stop": if not started: print("Car is already stoped!") else: started = False print("Car stopped.") elif command == "help": print(''' start - to start the car stop - to stop the car quit - to quit ''') elif command == "quit": break else: print("I don't understand that!")