注释
当行注视:# 被注释内容
多行注释:""" 被注释内容 """
用户输入
1
2
3
4
5
6
7
|
#!/usr/bin/env python
#_*_coding:utf-8_*_
#name = raw_input("What is your name?") #only on python 2.x
name
=
input
(
"What is your name?"
)
print
(
"Hello "
+
name )
|
输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法,即:
1
2
3
4
5
6
7
8
9
10
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import
getpass
# 将用户输入的内容赋值给 name 变量
pwd
=
getpass.getpass(
"请输入密码:"
)
# 打印输入的内容
print
(pwd)
|
说明:
getpass不能在pycharm中使用
购物车小程序:
goodslist=[ ("apple",5), ("iphone",8000), ("bike",800), ("book",80), ("coffee",35) ] buygoods=[] i=1 salary=input("please enter your salary:") if salary.isdigit(): salary=int(salary) while salary > 0: for index,item in enumerate(goodslist): print(index,item) your_choice=input("enter the goodsnumber:") if your_choice.isdigit(): your_choice=int(your_choice) if your_choice<len(goodslist) and your_choice>-1: p_item=goodslist[your_choice] if salary>=p_item[1]: salary-=p_item[1] buygoods.append(p_item) print("Added %s into shopping cart, your current balance is\033[31;1m%s\033[0m" %(p_item[0],salary)) else: print("your balance is not enough \033[41;1m你的余额只剩[%s]\033[0m" %(salary)) else: print("you enter the wrong number, no goods can be choose!") elif your_choice=="q": print("----------shop list-----") for p in buygoods: print(p) print("当前余额为\033[031;1m%s\033[0m" %(salary)) exit() else: print("invalid option") else: print("you enter muest be a number!")