1. 编写程序:calc.py 要求用户输入1到100之间数字并判断,输入符合要求打印“你妹好漂亮”,不符合要求则打印“你大爷好丑”
temp = input("please input a number between 1 to 100:")
num = int(temp)
if 1 <= num <= 100:
print("your sister is very beautiful!")
else:
print("your grandfather is clown")
2. 如果非要在原始字符串结尾输入反斜杠,可以如何灵活处理?
>>> str = r'C:\Program Files\FishC\Good''\\'
>>> print(str)
C:\Program Files\FishC\Good\
>>>
3. 请问以下代码会打印多少次“我爱鱼C!”
>>> while 'C':
print('我爱鱼C!')
答:无数次
4. 请问以下代码会打印多少次“我爱鱼C!”
>>> i = 10
>>> while i:
print('我爱鱼C!')
i = i - 1
我爱鱼C!
我爱鱼C!
我爱鱼C!
我爱鱼C!
我爱鱼C!
我爱鱼C!
我爱鱼C!
我爱鱼C!
我爱鱼C!
我爱鱼C!
答:10次
5. 请写出与 10 < cost < 50 等价的表达式
(10 < cost) and (cost < 50)
6. Python3 中,一行可以书写多个语句吗?
可以,语句之间用分号隔开即可,不妨试试:
>>> print('I love fishc');print('very much!')
7. Python3 中,一个语句可以分成多行书写吗?
可以,一行过长的语句可以使用反斜杠或者括号分解成几行,不妨试试:
- >>> 3 > 4 and \
- 1 < 2
或者
- >>> ( 3 > 4 and
- 1 < 2 )
8. 请问Python的 and 操作符 和C语言的 && 操作符 有何不同?【该题针对有C或C++基础的朋友】
python中没有&&操作符,python中的and,返回值不一样,如:
10 and 20 返回20
10 or 20 返回 10
9. 听说过“短路逻辑(short-circuit logic)”吗?
逻辑操作符有个有趣的特性:在不需要求值的时候不进行操作。这么说可能比较“高深”,举个例子,表达式 x and y,需要 x 和 y 两个变量同时为真(True)的时候,结果才为真。因此,如果当 x 变量得知是假(False)的时候,表达式就会立刻返回 False,而不用去管 y 变量的值。
这种行为被称为短路逻辑(short-circuit logic)或者惰性求值(lazy evaluation),这种行为同样也应用与 or 操作符,这个后边的课程小甲鱼会讲到,不急。
实际上,Python 的做法是如果 x 为假,表达式会返回 x 的值(0),否则它就会返回 y 的值(例子参考楼上那题)。
0. 完善第二个改进要求(为用户提供三次机会尝试,机会用完或者用户猜中答案均退出循环)并改进视频中小甲鱼的代码。
需要注意的地方在代码中已经注释!
- import random
- times = 3
- secret = random.randint(1,10)
- print('------------------我爱鱼C工作室------------------')
- # 这里先给guess赋值(赋一个绝对不等于secret的值)
- guess = 0
- # print()默认是打印完字符串会自动添加一个换行符,end=" "参数告诉print()用空格代替换行
- # 嗯,小甲鱼觉得富有创意的你应该会尝试用 end="JJ"?
- print("不妨猜一下小甲鱼现在心里想的是哪个数字:", end=" ")
- while (guess != secret) and (times > 0):
- temp = input()
- guess = int(temp)
- times = times - 1 # 用户每输入一次,可用机会就-1
- if guess == secret:
- print("我草,你是小甲鱼心里的蛔虫吗?!")
- print("哼,猜中了也没有奖励!")
- else:
- if guess > secret:
- print("哥,大了大了~~~")
- else:
- print("嘿,小了,小了~~~")
- if times > 0:
- print("再试一次吧:", end=" ")
- else:
- print("机会用光咯T_T")
- print("游戏结束,不玩啦^_^")
1. 尝试写代码实现以下截图功能:
- temp = input('请输入一个整数:')
- number = int(temp)
- i = 1
- while number:
- print(i)
- i = i + 1
- number = number - 1
2. 尝试写代码实现以下截图功能:
- temp = input('请输入一个整数:')
- number = int(temp)
- while number:
- i = number - 1
- while i:
- print(' ', end = '')
- i = i - 1
- j = number
- while j:
- print('*', end = '')
- j = j - 1
- print()
- number = number - 1