[Python]小甲魚Python視頻第004課(改進我們的小游戲)課后題及參考解答


# -*- coding: utf-8 -*-
"""
Created on Mon Mar  4 22:36:14 2019

@author: fengs
"""

"""
測試題:
0. 請問以下代碼會打印多少次“我愛魚C!”
while 'C':
    print('我愛魚C!')

一直打印

1.請問以下代碼會打印多少次“我愛魚C!
i = 10
while i:
    print('我愛魚C!')
    i = i - 1
    
打印10-1+1次,共計10次

2. 請寫出與 10 < cost < 50 等價的表達式
    cost > 10 and cost < 50
    
3. Python3 中,一行可以書寫多個語句嗎?
    可以,用分號隔開即可
    
4. Python3 中,一個語句可以分成多行書寫嗎?
    可以,用反斜杠續行即可

5. 請問Python的 and 操作符 和C語言的 && 操作符 有何不同?【該題針對有C或C++基礎的朋友】
    C/C++ 中的 && 返回的是邏輯值只會是0或者1;
    Python的and操作符也是邏輯運算,但結果未必是Ture 或者False,若布爾上下文的某個值為假,就返回第一個假值,若所有值為真就返回最后一個真值
6. 聽說過“短路邏輯(short-circuit logic)”嗎?
    a and b,若a已經為非真邏輯,就不在對b進行運算,直接判定表達式的值為a
    a or b,若a已經為真邏輯,就不再對b進行運算,直接判定表達式的值為b


動動手:
0. 完善第二個改進要求(為用戶提供三次機會嘗試,機會用完或者用戶猜中答案均退出循環)並改進視頻中小甲魚的代碼。

1. 嘗試寫代碼實現以下截圖功能:
    見樓下

2. 嘗試寫代碼實現以下截圖功能:
    見樓下


3. 請寫下這一節課你學習到的內容:格式不限,回憶並復述是加強記憶的好方式!
    .....


"""


#測試題:5,6
print(None and 1);
print([] and 1);
print([1] and [1,2,3]);

print(None or 1);
print([] or 1);
print([1] or [1,2,3]);

#動動手0
import random as rd;
max_times = 3;
secret = rd.randint(1,10);
user_input = 0;
guess_times = 0;
print('猜一猜:')
while True:
    guess_times += 1;
    user_input = int(input());
    if user_input == secret:
        print('猜中了,真厲害');
        break;
    else:
        if user_input > secret:
            print('輸入較大');
        else:
            print('輸入較小');
    if guess_times == max_times:
        print('次數已經用完');
        break;
    else:
        print('繼續猜吧!');
print('游戲結束');


#動動手1
top = int(input('請輸入一個整數:'));
for i in range(top):
    print(i+1);
    
    
#動動手2:
top = int(input('請輸入一個整數:'));
while top:
    print(' '*top + '*'*top);
    top -= 1;

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM