標籤:string gpo 浮點 core 多行注釋 time 重複執行 方式 必須
一.賦值
# 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