1. 程式人生 > >python自動化第二天-python

python自動化第二天-python

time 密碼 偶數 int imp 自動 date 小白 是什麽

一.賦值
# print(‘HEllo world!‘)#顯示括號內內容
# name = ‘顧駿琪‘ # 定義變量就是為了在後邊還要用到他的值
#name="let’go"
#print (name)
#conent = ‘‘‘let’go “帥”‘‘‘#三引號有多行註釋的功能、也可定義字符串,字符串內需展示單引號和雙引號時
#print (conent)
#age= 122 #中文可以當變量名
#print(顧駿琪) #int 類型 不需要加引號
age = 10
name = ‘小白‘ #字符串類型 string
score = ‘89.22‘ #浮點型 float
print(type(age)) # type 看變量是什麽類型的
print(type(name))

二.條件判斷
#age = 18  #條件判斷語句  if。。。else  如果。。。那麽。。。
#if age<18:
# print(‘未成年人‘)
#else:
# print(‘成年人‘)

# age=input(‘請輸入你的年齡‘) #input 輸入
# age=int(age) #類型轉換
# if age<18:
# print(‘未成年人‘)
# else:
# print(‘成年人‘)
# score = input(‘請輸入你的成績‘) #多條件判斷
# score = int(score)
# if score > 90:
# print(‘優秀‘)
# elif score>=75 and score<90: #同時滿足兩個條件用and
# print(‘良好‘)
# elif score>=60 and score<75:#elif 多個條件判斷,用elif連接起來
# print(‘及格‘)
# else:
# print(‘不及格‘)

sex = input(‘請輸入性別;‘) #or 滿足任意一個條件
if sex!=‘男‘ and sex!=‘女‘: #判斷是否等於用 == 不等於用 !=
print(‘性別未知‘)
else:
print(‘性別合法‘)

練習
import random #隨機產生
num = random.randint(1,10)#隨機產生一個1-10之間的數字
print(num)
new_num = input(‘請輸入你要猜的數字是多少:‘)
new_num = int(new_num) #數據類型轉換
if new_num>num: #判斷
print(‘輸入大了‘)
elif new_num<num:
print(‘你輸入的數字太小了‘)
else:
print(‘性別合法‘)
三.for循環
# for i in range(101):#i默認從0開始每次循環加1   range ()循環次數
# if i%2==0:
# print(‘偶數是‘,i)
# if else
#while
#for
#字符串格式化
import datetime
today=datetime.date.today() #取當前系統日期
username=input(‘請輸入用戶名:‘)
#welcome=‘歡迎光臨‘+username #第一種方式
welcome=‘歡迎光臨:%s 今天的日期是%s‘%(username,today) #%s叫站位符
print(welcome)
# %s 字符串 %d 整數

四.while...else
# count=0
# while count<3:
# if count==2:
# print(‘22222‘)
#
# count+=1
# else:
# print(‘循環結束!!‘)
#while 循環對應一個else的時候,循環在正常結束之後才會執行他
num=5
count = 0 # 設置計數器
while count < 3:# 設置循環判斷
guess = input(‘請輸入你要猜的數字‘)
guess = int(guess)
if guess > num:
print(‘你猜大了‘)
#continue # 到這裏結束本次循環,繼續執行下次循環
elif guess < num:
print(‘你猜小了‘)
else:
print(‘你對了‘,num)
break
count = count + 1 # 設置計數器+1
else:
print(‘遊戲次數已到‘)
六.if循環
#重復的去做一件事情
#循環、叠代、遍歷 都指的是循環
#for
#while 循環


#while 必須有一個計數器
import random
num=random.randint(1,100)
count = 0 #設置計數器
while count<10: #設置循環判斷
import random # 隨機產生
guess = input(‘請輸入你要猜的數字‘)
guess = int(guess)
if guess > num:
print(‘你猜大了‘)
continue #到這裏結束本次循環,繼續執行下次循環
elif guess<num:
print(‘你猜小了‘)
continue
else:
print(‘你對了‘)
break
count=count +1 #設置計數器+1
#循環時,是在重復執行循環體內的東西
#break 再循環內遇到break 立即結束 不管循環有沒有結束
#continue 在循環內遇到 continue 那麽就結束本次循環,繼續進行下次循環
# count-=1
# count=count-1
# count+=1
# count=count+1
# count*=1
# count=count*1
else: #循環正常結束之後做的操作
print()

作業
import datetime
ty=datetime.date.today()
count=0
while count<3:
username = input(‘請輸入用戶名‘)
passwd = input(‘請輸入密碼‘)
name=‘gujunqi‘
wd=‘123456‘
u=‘‘
if username==name and passwd==wd and username != u and passwd!=u:
print(‘%s 歡迎登陸 今天的日期是:%s‘%(username,ty))
break
else:
print(‘賬號或密碼輸入錯誤‘)
count+=1
else:
print(‘你的次數已經用盡‘)
 

python自動化第二天-python