|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
count = 3
password = 'Fish.com'
while count:
passwordenetered = input('please enter password:')
if passwordenetered == password:
print('correct!')
break
elif '*'in passwordenetered:
print('please re-enter password without *,\
you have',count,'chances')
continue
else:
print('please re-enter password,you have ',count - 1,'chances')
count -= 1
请问这个count是内置函数的count吗?为什么换成别的比如number = 3 就无法运行?
另外 在print里的count 一定要写成',count,'的形式吗?
谢谢各位大佬
本帖最后由 Twilight6 于 2020-8-4 00:38 编辑
不是,这里代码只是个普通的变量名,但是 Python 确实有个方法为 count ,用来统计元素个数的
为什么换成别的比如number = 3 就无法运行?
可以运行的,如果你要替换代码中的一个变量,那么代码中的所有 count 都要一起改成 number 的
帮你改了下:
- number = 3
- password = 'Fish.com'
- while number:
- passwordenetered = input('please enter password:')
- if passwordenetered == password:
- print('correct!')
- break
- elif '*'in passwordenetered:
- print('please re-enter password without *,\
- you have', number, 'chances')
- continue
- else:
- print('please re-enter password,you have ', number - 1, 'chances')
- number -= 1
复制代码
另外 在print里的count 一定要写成',count,'的形式吗?
不一定的,可以格式化输出和转为字符串拼接比如:
% 百分号格式化
- print('please re-enter password without *,\
- you have %d chances'%count)
复制代码
f-string 格式化
- print(f'please re-enter password without *,\
- you have {count} chances')
复制代码
format 格式化
- print('please re-enter password without *,\
- you have {} chances'.format(count))
复制代码
字符串拼接
- print('please re-enter password without *,\
- you have '+str(count)+' chances')
复制代码
|
|