如何用python编写猜数字游戏
1. 利用while判断来制作一个猜数字的小游戏python
# while循环做的猜数游戏
import random
# 首先引入random包,随机生成一个数
result = random.randint(1, 10)
while True:
answer = int(input('请输入您猜的数:'))
if answer == result:
print('恭喜你答对了')
break
else:
print('猜错了')
运行结果如图:
2. python猜数游戏:在程序中预设一个随机数
import random
num = random.randint(0, 100) #随机数
N = 0 #访问扰轿次数
while True:
N += 1
x = int(input())
if x == num:
print("猜中了,用了{}次".format(N))
break
if x > num:
print("太大了"伍李乱)
else:
print("腔档太小了")
3. 如何用python写一个三个人猜数字的游戏
importrandom
defguess(player):
declare='!'
number=int(raw_input('Player%s-Enteranumberbetween1and99:'%player))
ifnumber<1:
printdeclare
elifnumber>99:
printdeclare
else:
pass
returnnumber
defgame():
i=1
count=[0,0,0]
falg=True
rambom_num=random.randrange(1,99)
whilefalg:
forplayerinrange(0,3):
number=guess(player+1)
count[player]=i
ifnumber>rambom_num:
print'Yourguessistoohigh!'
elifnumber<rambom_num:
print滑清源'Yourguessistoolow!'
else:
print'--------------------------------------'
print'Yourmadetherightguess!'
print'Thesecretnumberis%s'%number
forpin信态range(0,len(count)):
print'Player%s-Totalnumberofguesses:%s'%(p+1,count[p])
falg=False
break
i=i+1
game()
>正渗>>
Player1-Enteranumberbetween1and99:50
Yourguessistoohigh!
Player2-Enteranumberbetween1and99:40
Yourguessistoohigh!
Player3-Enteranumberbetween1and99:30
Yourguessistoohigh!
Player1-Enteranumberbetween1and99:22
Yourguessistoohigh!
Player2-Enteranumberbetween1and99:18
Yourguessistoohigh!
Player3-Enteranumberbetween1and99:11
Yourguessistoohigh!
Player1-Enteranumberbetween1and99:9
Yourguessistoohigh!
Player2-Enteranumberbetween1and99:5
Yourguessistoolow!
Player3-Enteranumberbetween1and99:6
Yourguessistoolow!
Player1-Enteranumberbetween1and99:7
Yourguessistoolow!
Player2-Enteranumberbetween1and99:8
--------------------------------------
Yourmadetherightguess!
Thesecretnumberis8
Player1-Totalnumberofguesses:4
Player2-Totalnumberofguesses:4
Player3-Totalnumberofguesses:3
4. python语言编写一个玩猜数的游戏.由程序产生1~1000间的随机数,玩游戏者可输入最多十次猜数
这个有验证码的意思,
不过楼主码扒的python经验少了, 如果上面输入的不是数字, 那么银慎程序会报错的, 别有用心的人就能知道程序是如何编锋模敬写的了, 这样不好啊
以后遇到python方面的问题, 可以帮忙搞定。
楼主看看网名
5. 用python实现猜数字
a='1459'
forxinrange(7):
迹逗逗j=0
b=str(input('请输入4个数字:'))
#判断输入的是否为4位
iflen(b)!=4:
print("输入有误")
break
#判断输入的是否为数字
try:
指清float(b)
except:
姿卖print("输入有误")
break
foriinrange(4):
ifb[i]==a[i]:
j=j+1
print(str(j)+'A'+str(4-j)+'B')
ifj==4:
print("恭喜你赢了")
break
print("游戏结束")
6. 用Python写个猜数字游戏
修改如下:#include#include#include#includevoidmain(){intx ,y,x,x ;srand((unsigned)time(NULL));y=rand()% ;printf("请早烂输入 ~ 之间的数字:");x = ;x = ;scanf("%d",&x);while( ){if(x>y){printf("在%d~%d之间陆早漏",x ,x);x =x;}if(x==y){printf("恭睁源喜!你踩中地雷了O(∩_∩)O~");eak;}if(x
7. 想用python编写一个猜数字的游戏,,可以设置猜数时间超过规定时间显示显示游戏超时,时间这一块怎么弄呢
导入time模块去用不就行了
8. 用python写一个猜数游戏
#-*-coding:utf-8-*-
importrandom
import渣掘誉sys
ifsys.version[0]=='3':raw_input=input
x=[1,2,3,4,5,6,7,8,9]
result=set(x)
whileTrue:
散洞y=x[:]
random.shuffle(y)
y=y[:random.randint(1,len(x))]
print("%s中有你想要的吗?"%y)
answer=raw_input()
ifanswer.upper()in['Y','YES']:
result=result.intersection(y)
iflen(result)==1:
如段print("answeris:%s"%(list(result))[0])
break
9. 用python怎么实现一个猜字游戏
word="Telephone"
word=str.lower(word)
#g储存单词巧卖
g="* * * * * * * * *"
#把g变成个list
g=g.split()
for i in range(len(word)):
for n in range(len(g)):
print g[n],
print ""
guess=raw_input("Enter a letter: ")
#让用户输入个字母孝岁逗,如果一样就replace原来的
for j in range(len(word)):
if(word[j]==guess):
g[j]=guess
print ""
空格都没了。。。你自己indent...
这个好用不?给分不?雀雹
10. 初学python,被作业难到了,做一个猜数游戏,给十次机会,写完运行不出来,求大佬看看
import random
target=random.randint(1,1000)
count=0
while True:
try:
guess=eval(input("猜猜这个数是什么,一共有10次机会哦"))
except:
continue
print("请输入一个整数")
if guess<target:
print("猜小了")
count=+1
李携 elif guess>target:
print("猜大了")
count=+1
elif count==10:
销枣 print("机会用完了,欢迎下次再来!")
break
else:
print("猜对了,正确答案为"哪斗伏,target,"/n","一共猜了{}次".format(count))
break