python中猜数字小游戏

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

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")。

相关文档
最新文档