然后本次循环结束,再次进入 while 循环中的条件测试。
输入描述:
保证每一行的输入只有浮点数或字符串’quit’,且保证数字合法,范围在[0, 3]。
输出描述:
按题目描述进行输出即可。
示例1
输入:
0.5
1.2
quit
答案:
解法一
while True:
try:
print("Please tell me your height!\nEnter 'quit' to end the program.")
a = input()
if a == 'quit':
break
elif float(a) < 1.0:
print('Your admission cost is 0 yuan.')
elif 1.0 < float(a) <= 1.2:
print('Your admission cost is 80 yuan.')
else:
print('Your admission cost is 150 yuan.')
except:
break
解法二
operators_dict = {'<': 'less than','==': 'equal'}
print('Here is the original dict:')
for k in sorted(operators_dict):
print(f'Operator {k} means {operators\_dict[k]}.')
print()
operators_dict['>'] = 'greater than'
print('The dict was changed to:')
for k in sorted(operators_dict):
print(f'Operator {k} means {operators\_dict[k]}.')
题目二:寻找被污染的字符串
小明有一份字符串my_str = ‘I am$ N
i
u
n
i
u
iuniu
iuniu, an$d I
a
m
s
t
u
am stu
amstudying in
N
o
w
Now
Nowcoder!’ 被污染了,其中出现了很多奇怪的
符号。现请你使用
s
p
l
i
t
函数将这份字符串从符号
符号。 现请你使用split函数将这份字符串从符号
符号。现请你使用split函数将这份字符串从符号处分割成众多字符列表,记录在my_list中,并使用print函数直接打印my_list中的结果。
然后再使用join函数将my_list中的分段字符串重新连接成一个新的字符串,并使用print函数输出。
解法一
my_str = 'I am$ N$iuniu$, an$d I $am stu$dying in $Now$coder!'
my_list=my_str.split('$')
print(my_list)
my_str_new=''.join(my_list)
print(my_str_new)
解法二
a = ("I am$ N$iuniu$, an$d I $am stu$dying in $Now$coder!").split("$");
print(a)
print("".join(a))
题目三:实现计算求最大公约数和最小公倍数的函数。
答案:
def gcd(x, y):
(x, y) = (y, x) if x > y else (x, y)
for factor in range(x, 0, -1):
if x % factor == 0 and y % factor == 0:
return factor
def lcm(x, y):
return x \* y // gcd(x, y)
### 最后
不知道你们用的什么环境,我一般都是用的Python3.6环境和pycharm解释器,没有软件,或者没有资料,没人解答问题,都可以免费领取(包括今天的代码),过几天我还会做个视频教程出来,有需要也可以领取~
给大家准备的学习资料包括但不限于:
Python 环境、pycharm编辑器/永久激活/翻译插件
python 零基础视频教程
Python 界面开发实战教程
Python 爬虫实战教程
Python 数据分析实战教程
python 游戏开发实战教程
Python 电子书100本
Python 学习路线规划

**了解详情:https://docs.qq.com/doc/DSnl3ZGlhT1RDaVhV**