分別用while迴圈和for迴圈來玩轉猜數字游戲 1.對於限定猜數字次數 (1)while迴圈 number = 56 count = 0while count < 3: guess_number = int(input("guess number:")) if guess_number == nu ...
分別用while迴圈和for迴圈來玩轉猜數字游戲
1.對於限定猜數字次數
(1)while迴圈
number = 56
count = 0
while count < 3:
guess_number = int(input("guess number:"))
if guess_number == number:
print("Yes,you got it.")
break #break 跳出當前迴圈
elif guess_number > number:
print("think smaller...")
else:
print("think bigger!")
count +=1
else:
print("you have tried too many times..fuck off")
(2)for 迴圈
number = 56
for i in range(3):
guess_number = int(input("guess number:"))
if guess_number == number:
print("Yes,you got it.")
break
elif guess_number > number:
print("think smaller...")
else:
print("think bigger!")
else:
print("you have tried too many times..fuck off")
2. 任性玩 ,不限制次數
number = 56
count = 0
while count < 3:
guess_number = int(input("guess number:"))
if guess_number == number:
print("Yes,you got it.")
break
elif guess_number > number:
print("think smaller...")
else:
print("think bigger!")
count +=1
if count == 3:
countine_confirm = input("do you want to keep guessing") #如果猜錯3次,提示要不要退出游戲 ,輸入n表示退出
if countine_confirm !='n':
count = 0