———-CH04 homework———-
0.请问以下代码会打印多少次“我爱鱼C!”
Answer: 无限次,因为’C’ = 67 == truth,满足while的循环条件
1.请问以下代码会打印多少次“我爱鱼C!”
1. i = 10
2. while i:
3. print('我爱鱼C!')
4. i = i - 1
Answer: 10次
- 请写出与 10 < cost < 50 等价的表达式
Answer: 10 < cost and cost < 50
- Python3 中,一行可以书写多个语句吗?
Answer: 用分号隔开
e.g. >>> print(‘I love you’);print(5)
- Python3 中,一个语句可以分成多行书写吗?
Answer: 反斜杠’\’ 或者 括号’()’
e.g.
>>> 5>4 and \
6>3
>>> (3>2 and
2>1)
5.请问Python的 and 操作符 和 C语言的 && 操作符 有何不同?【该题针对有C或C++基础的朋友】
Answer:
C && test
#include
int main()
{
int a = 5;
int b = 3;
int c = 1;
printf("The result of ( a > b && b > c) is %d\n", (a > b) && (b > c));
return 0;
}
gcc test
The result of ( a > b && b > c) is 1
请按任意键继续. . .
结论:C语言中 && 操作符是逻辑运算符,备注:C语言中 & 是位操作符
Python Test
>>> 1 and 0
0
>>> 0 and 0
0
>>> 1 and 1
1
and 是逻辑预算符,同时也是bool运算符,>>> (3>2 and 2>1) 返回 True
6 听说过“短路逻辑(short-circuit logic)”吗?
Answer: 逻辑操作符有个有趣的特性:在不需要求值的时候不进行操作。这么说可能比较“高深”, ‘不做无用功’
e.g.表达式 x and y,需要 x 和 y 两个变量同时为真(True)的时候,结果才为真。
因此,如果当 x 变量得知是假(False)的时候,表达式就会立刻返回 False,而不用去管 y 变量的值。
这种行为被称为短路逻辑(short-circuit logic)或者惰性求值(lazy evaluation)
Practice
1.
The First method
import random
secret = random.randint(1, 100)
print('----------Talk to me wow----------')
temp = input('Please enter a number(1~100):')
guess = int(temp)
count = 0
while guess != secret:
if count > 6:
print('sorry! You guess 6 times')
print('Thanks\n')
break
if guess > secret:
print('You guess bigger')
count = count + 1
else:
print('You guess smaller')
count = count + 1
print('You have 7 times guess and You have', 7 - count, 'times')
temp = input('Please enter a number(1~100) again :')
guess = int(temp)
if guess == secret:
print('Wonderful! You guess right')
else:
if guess > secret:
print('You guess bigger')
count = count + 1
else:
print('You guess smaller')
count = count + 1
print('You have 7 times guess and You have', 7 - count, 'times')
temp = input('Please enter a number(1~100) again :')
guess = int(temp)
print('Congratulation!')
print('Game Over')
The second method:
import random
secret = random.randint(1, 100)
print('----------Talk to me wow----------')
guess = 0
count = 7
while guess != secret and count > 0:
print('All 7 times, and You still have ', count, ' chances')
temp = input('Please enter a number(1~100):')
guess = int(temp)
count = count - 1
if guess == secret:
print('Wonderful! Congration!')
else:
if guess > secret:
print('You guess bigger!')
else:
print('You guess smaller!')
if count > 0:
print('You can continue!')
else:
print('No chances! Bye')
print('Game Over')
TEST resu----------Talk to me wow----------
All 7 times, and You still have 7 chances
Please enter a number(1~100):50
You guess smaller!
You can continue!
All 7 times, and You still have 6 chances
Please enter a number(1~100):75
You guess smaller!
You can continue!
All 7 times, and You still have 5 chances
Please enter a number(1~100):88
You guess bigger!
You can continue!
All 7 times, and You still have 4 chances
Please enter a number(1~100):82
You guess bigger!
You can continue!
All 7 times, and You still have 3 chances
Please enter a number(1~100):78
You guess smaller!
You can continue!
All 7 times, and You still have 2 chances
Please enter a number(1~100):80
You guess smaller!
You can continue!
All 7 times, and You still have 1 chances
Please enter a number(1~100):81
Wonderful! Congration!
Game Over
2.
temp = input('Please enter a number(1~10): ')
num = int(temp)
i = 1
while num:
print(i)
i = i + 1
number = number -1
3.
temp = input('please enter a number(1~10): ')
num = int(temp)
i = num
if num > 10 or num < 1:
print('number 1~10 !!!')
else:
while i:
print('*' * i)
i = i - 1