Python学习3

第七章 用户输入和while循环

7.1 函数input()的工作原理

动手试一试:

carname=input('Please input the car you want to hire:')

print('Let me see if I can find you a '+carname.title())

users=input('please input how many people:')

if int(users)>8:

    print('no table now')

else:

    print('have table')

numbers=input('please enter the number:')

if int(numbers) % 10 == 0:

    print('this number Can be divided by 10!')

else:

    print('this number Can not be divided by 10!')

7.2 while循环

动手试一试:

#7-4 比萨配料

prompt = "\nPlease enter the name of pizza you want to add:"

prompt += "\n(Enter 'quit' when you are finished.)"

message = ""

while message != 'quit':

    message = input(prompt)

    print("We will add "+message+" to your pizza!")

prompt = "\nPlease enter the age of you,I will tell you the price of cinema ticket"

prompt += "\n(Enter 'quit' when you are finished.)"

age = ""

while age != 'quit':

    age = input(prompt)

    if age != 'quit':

        if int(age) < 3:

            print("You are "+age+" 22years old,you are free! ")

        if int(age) >= 3 and int(age) <= 12:

            print("You are "+age+" years old,you should pay 10 dollars! ")

        if int(age) > 12:

            print("You are "+age+" years old,you should pay 15 dollars! ")

prompt = "\nPlease enter the name of pizza you want to add:"

prompt += "\n(Enter 'quit' when you are finished.)"

message = ""

while message != 'quit':

    message = input(prompt)

    if message != 'quit':

        print("We will add "+message+" to your pizza!")


prompt = "\nPlease enter the age of you,I will tell you the price of cinema ticket"

prompt += "\n(Enter 'quit' when you are finished.)"

active = True

while active:

    age = input(prompt)


    if age == 'quit':

        active = False

    else:

        if int(age) < 3:

            print("You are "+age+" 22years old,you are free! ")

        if int(age) >= 3 and int(age) <= 12:

            print("You are "+age+" years old,you should pay 10 dollars! ")

        if int(age) > 12:

            print("You are "+age+" years old,you should pay 15 dollars! ")


prompt = "\nPlease enter the age of you,I will tell you the price of cinema ticket"

prompt += "\n(Enter 'quit' when you are finished.)"

while True:

    age = input(prompt)


    if age == 'quit':

        break

    else:

        if int(age) < 3:

            print("You are "+age+" 22years old,you are free! ")

        if int(age) >= 3 and int(age) <= 12:

            print("You are "+age+" years old,you should pay 10 dollars! ")

        if int(age) > 12:

            print("You are "+age+" years old,you should pay 15 dollars! ")

7.3 使用while循环来处理列表和字典