1. 程式人生 > >python之helloworld

python之helloworld

hone pwd 運行 name 空格 for 遍歷 名稱 輸入密碼

python3默認字符集默認是Unicode
python2默認字符集默認是ASCII

print(‘hello_world!‘)

name = ‘小老虎‘
age = 27

print("let‘s go!")
print(‘你真"帥啊"‘)
print(‘‘‘let‘s go "帥"‘‘‘)

‘‘‘
這是一段多行註釋
用三引號就可以了
‘‘‘

需要註意python中不能使用關鍵字做變量名

name1 = input(‘請輸入你的名字:‘)
print(‘你的名字是:‘, name1)

分支語句 if elif else
# username = input(‘請輸入您的用戶名:‘)
# password = input(‘請輸入您的密碼:‘)
# if username == ‘wangsilei‘ and password == 123456:
# print(‘歡迎光臨!‘)
# else:
# print(‘賬號/密碼錯誤!‘)

# input接受到的都是str類型的
# score = input(‘請輸入你的分數:‘)
# if int(score) < 60:
# if int(score) >= 50:
# print(‘50-60‘)
# else:
# print(‘太低了!‘)
# print(‘不及格!‘)
# elif 60 <= int(score) < 80:
# print(‘及格!‘)
# elif 80 <= int(score) < 90:
# print(‘良好!‘)
# else:
# print(‘優秀!‘)

# == != < > <= >=比較運算符

sex = input(‘請輸入你的性別:‘)
if sex == ‘男‘ or sex == ‘女‘:
print(‘性別輸入正確!‘)
else:
print(‘性別輸入有誤!‘)

# 布爾類型 True False

for、while循環
import random


# 循環 叠代 遍歷
# for while

# break結束循環
# continue結束本次循環,繼續下一次循環
# count = 0
# while count < 10:
# print(‘哈哈哈哈‘)
# count += 1
# print(‘666‘)
# break
# else: # 循環正常結束運行它
# print(‘done!‘)

# count = 0
# while count < 3:
# username = input(‘請輸入名字:‘)
# password = input(‘請輸入密碼:‘)
# if username == ‘wangsilei‘ and password == ‘123456‘:
# print(‘登錄成功!‘)
# break
# else:
# print(‘名字/密碼錯誤!‘)
# count += 1
# else:
# print(‘錯誤次數過多!‘)
# for i in range(10):
# print(i)
random_number = random.randint(1, 1000)
for i in range(3):
number = int(input(‘請輸入你猜的數字:‘))
if number > random_number:
print(‘你猜的太大了!‘)
elif number < random_number:
print(‘你猜的太小了!‘)
else:
print(‘你猜對了,答案是:‘, random_number)
break

猜數字遊戲
import random


# 以下代碼不加continue也可以
random_number = random.randint(1, 1000)
count = 0
while count < 7:
count += 1
number = int(input(‘請輸入你猜的數字:‘))
if number > random_number:
print(‘你猜的太大了!‘)
continue
elif number < random_number:
print(‘你猜的太小了!‘)
continue
else:
print(‘你猜對了,答案是:‘, random_number)
break

字符串格式化
username = input(‘請輸入你的名字:‘)
time = ‘2017-12-17 17:19:00‘
print(‘歡迎光臨‘ + username) # 通過+號拼接兩個字符串
print(‘歡迎光臨%s,時間是%s‘ % (username, time)) # 這種方式效率高
print(‘歡迎光臨{},時間是{}‘.format(username, time)) # 這種比第二種好
print(‘歡迎光臨{name},時間是{date}‘.format(name=username, date=time))

# %s後跟字符串 %d後跟整數 %f小數類型

number = 123
print(‘%.2f‘ % number) # 保留兩位小數

登錄小程序
username = ‘wangsilei‘
password = 123456

for i in range(3):
name = input(‘請輸入賬戶名稱:‘)
pwd = input(‘請輸入賬戶密碼:‘)
if name == username and int(pwd) == password:
print(‘{}歡迎登錄‘.format(name))
break
elif name == ‘‘ or pwd == ‘‘:
print(‘賬戶名稱/賬戶密碼不可為空‘)
elif name.isspace() or pwd.isspace():
print(‘賬戶名稱/賬戶密碼不可為空格‘)
else:
print(‘賬戶名稱/密碼錯誤!‘)

生成手機號碼並寫入txt小程序
import random


phone_lists = [‘130‘, ‘131‘, ‘132‘, ‘133‘, ‘134‘, ‘135‘, ‘136‘, ‘170‘, ‘180‘]
phone_str = ‘0123456789‘

n = int(input(‘生成多少個隨機的電話號碼:‘))
with open(r‘C:\Users\wangsilei\Desktop\phone.txt‘, ‘w‘) as f:
for i in range(n):
phone_numbers = random.choice(phone_lists) + ‘‘.join(random.choice(phone_str) for j in range(8)) + ‘\n‘
f.write(phone_numbers)

python之helloworld