最近几天在看些基础知识,然后遇到一个小甲鱼老师,修改了下他的课堂例子。
大概就是猜数字,有三次机会,如果输入的不是数字就直接结束,如果是数字就会判断是否是心里的数字,提醒大小。
import random
#Guess = input("請猜猜我心裏想的是什麽數字:")
RightGuess = random.randint(0,10)
Guess = input("請猜猜我心裏想的是什麽數字:")
for i in range(3) :
try:
UserGuess = int(Guess)
a = isinstance(UserGuess, int)
a !=True
except ValueError:
print("你輸入有誤!")
break
else:
if(UserGuess >RightGuess):
print("你猜大啦!")
Guess = input("請繼續:")
try:
UserGuess = int(Guess)
a = isinstance(UserGuess, int)
a != True
except ValueError:
print("你輸入有誤!")
break
if(i==1):
print("猜錯三次了!")
break
elif(UserGuess <RightGuess):
print("你猜小啦!")
Guess = input("請繼續:")
try:
UserGuess = int(Guess)
a = isinstance(UserGuess, int)
a != True
except ValueError:
print("你輸入有誤!")
break
UserGuess = int(Guess)
if (i == 1):
print("猜錯三次了!")
break
else:
print("哇,你是我心裏的蛔蟲嗎?")
print("但是猜對了也沒有獎勵!")
break
print("游戲結束了!")