如何用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