1. 程式人生 > >異想-天開 python---while、for、if-else 迴圈學習

異想-天開 python---while、for、if-else 迴圈學習

for迴圈:

for i in range(10):  # i迴圈10次
print('------',i)
for j in range(10):
print(j)
if j > 2 :
break #跳出迴圈體

while迴圈:
age_oldboy_guess = 50
conet = 0
# age_guess = int(input('age_oldboy_guess:'))
# if age_oldboy_guess == age_guess:
# print('猜對了!')
# print('get over!') # 這一段已經寫多餘了。。

# exit()
# else:
# print('猜錯了!')
while conet < 3 :
# for i in range(3):
age_guess = int(input('age_oldboy_guess:'))
if age_oldboy_guess == age_guess:
print('猜對了!')
break
else:
print('猜錯了!')
conet += 1
# else:
# print("遊戲結束!")

if conet == 3:
i = input('還想玩嗎?繼續請輸入Y/y:')

if i == 'Y' or i =='y':
conet = 0
else:
break
print('get over!')

if--else迴圈:
name = input('name:')
sex = input('sex:')
age = int(input('age:'))

if sex == "f":
if age < 28 :
print("I LOVE girl!")
else:
print('2222!')
else:
print("1111")