天天看點

python程式設計:從入門到實踐 第七章練習題

7-3  10的整數倍:讓使用者輸入一個數字,并指出這個數字是否是10的整數倍。

num = int(input("please input a number: "))
if num % 10 == 0:
    print(num,'is an Integer multiple of 10')
else:
    print(num,"isn't an Integer multiple of 10")
           
In [2]: runfile('E:')

please input a number: 12
12 isn't an Integer multiple of 10
           

7-4 編寫一個循環,提示使用者輸入一系列的比薩配料,并在使用者輸入'quit' 時結束循環。每當使用者輸入一種配料後,都列印一條消息,說我們會在比薩

中添加這種配料。

while True:
    ingre = input("Please input an ingredient to Pizza: ")
    if ingre == 'quit':
        break
    else:
        print('we will add this ingredient to the Pizza')
           
In [3]: runfile('E:')

Please input an ingredient to Pizza: egg
we will add this ingredient to the Pizza

Please input an ingredient to Pizza: quit
           

7-10 編寫一個程式,調查使用者夢想的度假勝地。使用類似于“If you could visit one place in the world, where would you go?”的提示,并編寫一個列印調查

結果的代碼塊。

responses = {}
active = True

while active:
    name = input("What is your name? ")
    response = input('If you could visit one place in the world, where would you go? ')
    
    responses[name] = response
    
    while True:
        repeat = input('Would you like to get another response? (y/n) ')
        if repeat.lower() == 'n':
            active = False
            break
        elif repeat.lower() == 'y':
            active = True
            break
            
print('--- Poll Results ---')
for name, response in responses.items():
    print(name + ' like to visit ' + response + '.')
           
In [4]: runfile('E:')

What is your name? hujie

If you could visit one place in the world, where would you go? beijing

Would you like to get another response? (y/n) j

Would you like to get another response? (y/n) k

Would you like to get another response? (y/n) y

What is your name? huer

If you could visit one place in the world, where would you go? nanjing

Would you like to get another response? (y/n) y

What is your name? kl

If you could visit one place in the world, where would you go? xi'an

Would you like to get another response? (y/n) n
--- Poll Results ---
hujie like to visit beijing.
huer like to visit nanjing.
kl like to visit xi'an.