小甲魚Python視頻第004講:(改進我們的小游戲)課后習題及參考答案


------------恢復內容開始------------

測試題:

0. 請問以下代碼會打印多少次“我愛魚C!”
    while 'C':
       print('我愛魚C!')
 
一直打印
 
1.請問以下代碼會打印多少次“我愛魚C!
  i = 10
  while i:
     print('我愛魚C!')
     i = i - 1
     
打印10-1+1次,共計10次
eg:
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. 完善第二個改進要求(為用戶提供三次機會嘗試,機會用完或者用戶猜中答案均退出循環)並改進視頻中小甲魚的代碼。
 
"""用python設計第一個游戲---改進1"""
# 第一部分
# 引入隨機數 random模塊  把8替換掉
import random
num1 = random.randint(1,10)
times = 3

temp = input ("隨意猜一個數字(最多三次機會):")
guess = int (temp)
while (guess!= num1) and (times>0) :
        if guess == num1:
            print("right")
        else:
            if guess > num1 :
                print("大了")
            else:
                print("小了")
            times = times - 1
            if times>0:
                print("最后第" + str(times) + "次機會!")
                temp = input("請重新輸入:")
                guess = int(temp)
            else:
                print("機會用光了··")
print("游戲結束")
1. 嘗試寫代碼實現以下截圖功能:
 
1 temp=int(input("請輸入一個整數:"))
2 number=temp
3 i=1
4 while number>0:
5     print(i)
6     i=i+1
7     number=number-1

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

 
          
方法1:
temp=int(input("請輸入一個整數:")) number=temp while number>0: print(" "*number+"*"*number) number=number-1
方法2:(小甲魚方法)
1
temp = input('請輸入一個整數:') 2 number = int(temp) 3 while number: 4 i = number - 1 5 while i: 6 print(' ', end = '') 7 i = i - 1 8 j = number 9 while j: 10 print('*', end = '') 11 j = j - 1 12 print() 13 number = number - 1
3. 請寫下這一節課你學習到的內容:格式不限,回憶並復述是加強記憶的好方式!
    測試題5,6
 
#測試題: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 ]);
 


免責聲明!

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



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