python中猜数字小游戏


 

1、原始游戏   (input 内建函数用于接收用户输入)

temp = input("please input an integer:") guess = int(temp) if guess == 8: print("you are right!") print("no gift evec though you guess right!") else: print("wrong!") print("right number is 8!") print("gave ove!")

 

 

 

2、改进小游戏

给出输入偏离8时的提示:

不限制用户执行程序的次数,直到猜中。

temp = input("please input the number:") guess = int(temp) while guess != 8: print("wrong!") if guess > 8: print("big!") else: print("small!") temp = input("try again:") guess = int(temp) print("you are right!") print("game over!")

 

 

3、改进小游戏

引入随机答案。

temp = input("please input an number:") guess = int(temp) import random secret = random.randint(1,10) while guess != secret: print("wrong!") if guess > secret: print("big!") else: print("small!") temp = input("try again:") guess = int(temp) print("you are right!") print("game over!")

 

 

 

4、改进小游戏

只给三次机会。

方法1:

temp = input("please input an number:") guess = int(temp) import random secret = random.randint(1,10) times = 0

while times < 3: if guess == secret: print("you are right!") break
    else: print("you are wrong!") if guess != secret: if guess > secret: print("big") else: print("small") times += 1
    if times < 3: temp = input("try again:") guess = int(temp) if times == 3: print("times over!")

 

方法2:

temp = input("please input an number:") guess = int(temp) import random secret = random.randint(1,10) times = 1

while guess != secret and times < 3: if guess > secret: print("big") else: print("small") temp = input("please try again:") guess = int(temp) times += 1

if times == 3 and guess != secret: print("time out!,game over") else: print("right")

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM